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!