Sum all the elements two matrices

6 visualizzazioni (ultimi 30 giorni)
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!!

Risposta accettata

Ameer Hamza
Ameer Hamza il 2 Mag 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

Più risposte (0)

Categorie

Scopri di più su Multidimensional Arrays in Help Center e File Exchange

Prodotti


Release

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by