Sum all the elements two matrices

12 次查看(过去 30 天)
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!!

采纳的回答

Ameer Hamza
Ameer Hamza 2020-5-2
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by