Sum all the elements two matrices

8 ビュー (過去 30 日間)
TOMAS CARRERA DE SOUZA
TOMAS CARRERA DE SOUZA 2020 年 5 月 2 日
コメント済み: Ameer Hamza 2020 年 5 月 3 日
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
  2 件のコメント
TOMAS CARRERA DE SOUZA
TOMAS CARRERA DE SOUZA 2020 年 5 月 3 日
Thanks!
Ameer Hamza
Ameer Hamza 2020 年 5 月 3 日
I am glad to be of help.

サインインしてコメントする。

その他の回答 (0 件)

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by