Matrix extension by inserting 0
古いコメントを表示
I want to extend matrix by inserting 0 like uploaded picture.
How can I do?


採用された回答
その他の回答 (1 件)
dpb
2022 年 3 月 15 日
M1=eye(4); % simply symmetric matrix to play with
N=2; % number columns/rows to insert
R=2;C=2; % starting row, column at which to insert
Z=zeros(size(M1,1),N); % zeros columns to insert; could be anything constant
M2=[M1(:,1:C) Z M1(:,C+1:end)]; % step 1; insert the columns
M2=[M2(1:R,:); [Z.' zeros(N)]; M2(R+1:end,:)]; % step 2; insert rows; must augment the Z to match new
The latter is quite simple to just place the original in the corner of a larger -- but there is a MATLAB syntax "trick" -- start with M1 again, then--
S=size(M1); S=S+N; % get size vector, augment to desired size
M2=M1; % start with original
M2(S(1),S(2))=0; % set outer bound value; ML automagically zero extends
カテゴリ
ヘルプ センター および File Exchange で Operators and Elementary Operations についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!