adding every element of an array with every element of another array?
Show older comments
dear all, i want to add array elements to every other element of another element of another array in simulink/matlab
Accepted Answer
More Answers (3)
Vishal Rane
on 30 Nov 2012
Edited: Vishal Rane
on 30 Nov 2012
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 Comments
Pranav
on 30 Nov 2012
Vishal Rane
on 30 Nov 2012
arrayfun(@(x)(x+A),B,'Un',0)
will work in that case but output will be a 1x4 cell arrray.
Pranav
on 3 Dec 2012
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,[])
Categories
Find more on Creating and Concatenating Matrices 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!