adding every element of an array with every element of another array?

11 次查看(过去 30 天)
dear all, i want to add array elements to every other element of another element of another array in simulink/matlab

采纳的回答

Andrei Bobrov
Andrei Bobrov 2012-11-30
Pranav wrote: "...i want to add elemnts of A to be added to every other elemnet of B. ie. 1+[5 6 7 8],2+[5 6 7 8],...so on..."
A = [ 1 2 3 4];
B = [ 5 6 7 8];
C = reshape(bsxfun(@plus,A,B.'),1,[]);

更多回答(3 个)

Vishal Rane
Vishal Rane 2012-11-30
编辑:Vishal Rane 2012-11-30
Do you need to do this in MATLAB code ?
A = [ 1 2 3 4]
B = [ 5 6 7 8]
C = A + B = [ 6 8 10 12 ]
Comment back if your requirement is different.
  3 个评论

请先登录,再进行评论。


Matt Fig
Matt Fig 2012-11-30
A = 0:4;
B = 10:16;
bsxfun(@plus,A,B')

Brandon
Brandon 2016-5-9
编辑:Brandon 2018-12-11
I found this conversation while looking for a faster way to accomplish this task. The code below also works but is just slightly slower in my code on my machine.
X=[A' ones(size(A'))];
Y=[ones(size(B)); B];
reshape(X*Y,1,[])

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by