Sum all the elements two matrices
2 次查看(过去 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!!
0 个评论
采纳的回答
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 Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!