Remove columns and rows of zeros from matlab matrix

I would like to remove some columns and rows from a large matrix. These are columns and rows that have all zeros. Is there any function in MATLAB that can do this for you pretty quickly? My matrices are sparse. I do like this:

% To remove all zero columns from A ind = find(sum(A,1)==0) ; A(:,ind) = [] ; % To remove all zeros rows from A ind = find(sum(A,2)==0) ; A(ind,:) = [] ; 

It would be nice to have a line of code for this, as I can repeat this task. Thanks

+6
source share
1 answer

Single line of code:

 A=A(any(X,2),any(X,1)) 

There is no need to use find , as you, you can directly index using logical vectors.

+7
source

All Articles