MATLAB: How can i add zeros to same specified matrix column and row

arrayzeros

Hi there,
I want to change my array A(5×5) to adding zeros to 4th and 5th columns and rows. So, output of A matrix can be (7×7) matrix.
A=[1,2,3,4,5;
6,7,8,9,10;
11,12,13,14,15;
16,17,18,19,20;
21,22,23,24,25]
to
A = [1,2,3,0,0,4,5;
6,7,8,0,0,9,10;
11,12,13,0,0,14,15;
0,0,0,0,0,0,0;
0,0,0,0,0,0,0;
16,17,18,0,0,19,20;
21,22,23,0,0,24,25]

Best Answer

[m,n] = size(A);
rc = [4,5]; % where to place zeros
N = 2; % 2 columns and rows
b = zeros(m+N,N+n);
R = setdiff(1:m+N,rc);
C = setdiff(1:n+N,rc);
b(R,C) = A % b is your new A