Sum all the elements two matrices

2 visualizaciones (últimos 30 días)
TOMAS CARRERA DE SOUZA
TOMAS CARRERA DE SOUZA el 2 de Mayo de 2020
Comentada: Ameer Hamza el 3 de Mayo de 2020
Hello, I am trying to create a new matrix based on the sum of all the elements of two input matrices.
My operation is similar to the Kronecker product, but insted of multiplying the elements, adding them.
For example,
Input
A= [a11 a12; a21 a22]
B= [b11 b12; b21 b22]
And I am trying to get this:
C= [a11+b11 a11+b12 a12+b11 a12+b12; a11+b21 a11+b22 a12+b21 a12+b22; a21+b11 a21+b12 a22+b11 a22+b12; a21+b21 a21+b22 a22+b21 a22+b22]
Or with numbers
A= [0 1; 2 3]
B= [4 5; 6 7]
C= [4 5 5 6; 6 7 7 8; 6 7 7 8; 8 9 9 10]
Is there any way to do this operation?
Thanks!!

Respuesta aceptada

Ameer Hamza
Ameer Hamza el 2 de Mayo de 2020
Try this
A = [0 1; 2 3];
B = [4 5; 6 7];
A_ = repelem(A, size(B,1), size(B,2));
B_ = repmat(B, size(A));
C = A_ + B_;
Result
>> C
C =
4 5 5 6
6 7 7 8
6 7 7 8
8 9 9 10
  2 comentarios
TOMAS CARRERA DE SOUZA
TOMAS CARRERA DE SOUZA el 3 de Mayo de 2020
Thanks!
Ameer Hamza
Ameer Hamza el 3 de Mayo de 2020
I am glad to be of help.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by