remove rows with all zeros

I have
a =
1 2 3
0 0 0
2 1 0
4 5 0
0 0 0
2 0 1
I need
b=
1 2 3
2 1 0
4 5 0
2 0 1

 Accepted Answer

b = a(any(a,2),:);

5 Comments

thanks Walter
walter cheers mate
Note: the above code treats nan values the same as zero values, so for example the row [0 nan nan] would also be removed. If that is not acceptable, then
b = a(any(a ~= 0,2),:);
Hi Walter,
Would you please explain this line?
b = a(any(a,2),:);
Thank you

Sign in to comment.

More Answers (1)

Geoff
Geoff on 6 Jun 2012

0 votes

Search facility on Answers shows this question is asked a lot... Here's one of the more recent.

Products

Community Treasure Hunt

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

Start Hunting!