Adding two matrixes with different row numbers.
1 回表示 (過去 30 日間)
古いコメントを表示
Santos García Rosado
2021 年 2 月 26 日
コメント済み: Santos García Rosado
2021 年 2 月 26 日
Hi Mathworks community!
I have a matrix A(mxn) and a matrix B(lxn) such as:
A = [1,2,3,4,5,6,7,8,9,10; 10,11,12,13,14,15,16,17,18,19; 19,20,21,22,23,24,25,26,27,28];
B = [1,2,3,4,5,6,7,8,9,10; 2,4,6,8,10,12,14,18,18,20];
I'd like to get an output such as:
out = [sum(A(1,:),B(1,:)); sum(A(1,:),B(2,:)); sum(A(2,:),B(1,:)); sum(A(2,:),B(2,:)); sum(A(3,:),B(1,:)); sum(A(3,:),B(2,:))];
Any idea about how I could code this efficiently?
Thank's in advance!
Santos García
0 件のコメント
採用された回答
Stephen23
2021 年 2 月 26 日
A = [1,2,3,4,5,6,7,8,9,10; 10,11,12,13,14,15,16,17,18,19; 19,20,21,22,23,24,25,26,27,28];
B = [1,2,3,4,5,6,7,8,9,10; 2,4,6,8,10,12,14,18,18,20];
[Y,X] = ndgrid(1:size(B,1),1:size(A,1));
out = A(X(:),:) + B(Y(:),:)
その他の回答 (1 件)
Hernia Baby
2021 年 2 月 26 日
Following your output image,
out = [sum(A(1,:),B(1,:)); sum(A(1,:),B(2,:)); sum(A(2,:),B(1,:)); sum(A(2,:),B(2,:)); sum(A(3,:),B(1,:)); sum(A(3,:),B(2,:))];
the code is like below
A = [1,2,3,4,5,6,7,8,9,10; 10,11,12,13,14,15,16,17,18,19; 19,20,21,22,23,24,25,26,27,28];
B = [1,2,3,4,5,6,7,8,9,10; 2,4,6,8,10,12,14,18,18,20];
A_sum = sum(A,2);
B_sum = sum(B,2);
for i = 1:length(A_sum)
for j = 1:length(B_sum)
C(i,j) = A_sum(i) + B_sum(j);
end
end
out = rehape(C,[],1);
参考
カテゴリ
Help Center および File Exchange で Fourier Analysis and Filtering についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!