MATLAB Answers

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

2 views (last 30 days)
Hi there,
I want to change my array A(5x5) to adding zeros to 4th and 5th columns and rows. So, output of A matrix can be (7x7) 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]

  0 Comments

Sign in to comment.

Accepted Answer

madhan ravi
madhan ravi on 30 May 2020
[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

More Answers (0)

Tags


Translated by