How do I apply a function to two vectors using 2 inputs, one from each vector?
Show older comments
I've got 2 vectors, and I have a function. However, the function contains sums. I need to sum all the components of x, but also sum x1*y1 and x2*y2, etc.
How can I run a function so that it uses 2 vectors, but sums the entire vector?
1 Comment
What is wrong with the obvious answer of creating a function with two input arguments?
What you want sounds like just matrix multiplication though, e.g.
K>> x = [1 2 3]
x =
1 2 3
K>> y = [4 5 6]'
y =
4
5
6
K>> x * y
ans =
32
Answers (1)
Jan
on 7 Mar 2018
It is not really clear to me, what you are asking for. Maybe this helps you in spite of this:
x = rand(1, 5);
y = rand(1, 5);
function [xsum, ysum, xtimesy] = yourfcn(x, y)
xsum = sum(x);
ysum = sum(y);
xtimesy = x .* y;
end
Categories
Find more on Line Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!