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

11 visualizaciones (últimos 30 días)
dear all, i want to add array elements to every other element of another element of another array in simulink/matlab

Respuesta aceptada

Andrei Bobrov
Andrei Bobrov el 30 de Nov. de 2012
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,[]);

Más respuestas (3)

Vishal Rane
Vishal Rane el 30 de Nov. de 2012
Editada: Vishal Rane el 30 de Nov. de 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 comentarios
Vishal Rane
Vishal Rane el 30 de Nov. de 2012
arrayfun(@(x)(x+A),B,'Un',0)
will work in that case but output will be a 1x4 cell arrray.
Pranav
Pranav el 3 de Dic. de 2012
Thanks Vushal i will check for other array sizes.

Iniciar sesión para comentar.


Matt Fig
Matt Fig el 30 de Nov. de 2012
A = 0:4;
B = 10:16;
bsxfun(@plus,A,B')

Brandon
Brandon el 9 de Mayo de 2016
Editada: Brandon el 11 de Dic. de 2018
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,[])

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by