I want to add a 20 by 20 matrix to a 50 by 50 matrix ? the resuting matrix should be of 50 by 50 .

2 ビュー (過去 30 日間)
For Example : A=5*ones(50) & B=2*ones(20) , how should A+B be evaluated ?
  3 件のコメント
Amir
Amir 2015 年 2 月 26 日
Mr. Raghavendra did right what i wanted ! But ur question was also in my mind to pad zeros with the smaller matrix. How would u do it
Guillaume
Guillaume 2015 年 2 月 26 日
Exactly, the way Raghavendra did it, or using padarray if you have the imaging toolbox.

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

採用された回答

RAGHAVENDRA
RAGHAVENDRA 2015 年 2 月 26 日
A=5*ones(50)
B=zeros(50);
B(1:20,1:20)=2*ones(20);
C=A+B;

その他の回答 (1 件)

Jos (10584)
Jos (10584) 2015 年 2 月 26 日
For arbitrary sized 2D matrices A and B:
% example data
A = ones(3,5)
B = 2*ones(4,2)
% engine
szA = size(A)
szB = size(B)
C = zeros(max([szA ; szB]))
C(1:szA(1),1:szA(2)) = A
C(1:szB(1),1:szB(2)) = C(1:szB(1),1:szB(2)) + B

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by