Matrix extension by inserting 0
Mostrar comentarios más antiguos
I want to extend matrix by inserting 0 like uploaded picture.
How can I do?


Respuesta aceptada
Más respuestas (1)
dpb
el 15 de Mzo. de 2022
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
Categorías
Más información sobre Operators and Elementary Operations en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!