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.

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

temp = A;
temp(any(A,2),any(A,1)) = Anew;
Anew = temp;

4 Comments

This will copy my old matrix A to Anew. I need the new matrix(Anew) of size(n-m)x(n-m) to be nxn matrix by adding those zero rows and columns in appropriate position as removed previously..
Did you try my posted code? What is the size of the result? Are the zero rows & columns where they were originally in the A matrix?
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
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)

Community Treasure Hunt

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

Start Hunting!