Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove zeros column and rows from a matrix matlab

Tags:

matlab

I would like to remove some columns and rows from a big matrix. Those are the columns and the rows which have all zeros values. Is there any function in MATLAB that can do it for you quite fast? My matrices are sparse. I am doing this way:

 % 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 may do this kind of task repeatedly. Thanks

like image 858
Yas Avatar asked Feb 15 '16 10:02

Yas


1 Answers

A single line of code would be:

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

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

like image 76
Daniel Avatar answered Oct 26 '22 23:10

Daniel