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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With