I have a nxn matrix(A) which has many zero rows and zero columns.. I removed the zero rows and columns using the command below such that Anew is now (n-m) x (n-m). After some computations on Anew matrix, I need to get Anew matrix back to nxn matrix.

8 views (last 30 days)
I am removing the zero rows and zero columns from my matrix A using the command:
Anew=A(any(A,2),any(A,1)) ;where A(nxn) and Anew((n-m)x(n-m)) where there are m zero rows/columns
After some computation on Anew, I need to add the zero rows and columns back to their original position so that Anew now becomes nxn..
How to do it? Please help

Accepted Answer

James Tursa
James Tursa on 25 Jan 2018
temp = A;
temp(any(A,2),any(A,1)) = Anew;
Anew = temp;
  4 Comments
SanthoshKumar C
SanthoshKumar C on 26 Jan 2018
Ur posted code will give Anew matrix of size nxn. But what I need is lil different.
Ex:
A=[1 0 2 0 3; 0 0 0 0 0; 4 0 5 0 6; 0 0 0 0 0; 7 0 8 0 9];
After the step,
Anew=A(any(A,2),any(A,1)) ;
Anew =
1 2 3
4 5 6
7 8 9
Now I am doing some computation on Anew such that modified Anew will be as follows:
Anew = Anew+5
Anew =
6 7 8
9 10 11
12 13 14
Last step: (Need to do this now) . Insert the zero rows and columns back to the original position in Anew such that Anew looks like this
Anew =
6 0 7 0 8
0 0 0 0 0
9 0 10 0 11
0 0 0 0 0
12 0 13 0 14
SanthoshKumar C
SanthoshKumar C on 26 Jan 2018
Since I am working on matrix inverse of a (300+)x(300+) matrix which can have a more number of zero rows and columns (ill-conditioned matrix ofcourse).. Need robust way to do this

Sign in to comment.

More Answers (1)

SanthoshKumar C
SanthoshKumar C on 26 Jan 2018
Thanks James Tursa.. I overlooked your answer.. Its working

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!