How do I apply a function to two vectors using 2 inputs, one from each vector?

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 个评论

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

请先登录,再进行评论。

回答(1 个)

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

类别

在 帮助中心 和 File Exchange 中查找有关 Line Plots 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by