Clear Filters
Clear Filters

Find rows containing N zeros in a matrix

2 views (last 30 days)
I have a matrix like:
mat =[1 2 3;
0 0 3;
1 2 0;
0 0 0;
2 0 0];
To find a row full of zeros I do:
mat(all(mat==0,2)
ans =
0 0 0
What can I do to find the rows which only two zeros?
To get this:
ans =
0 0 3
2 0 0

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 16 Jan 2014
Edited: Azzi Abdelmalek on 17 Jan 2014
mat(sum(mat==0,2)==2,:)
For general case
N=2
out=mat(sum(mat==0,2)==N,:)
  3 Comments
Giorgos Papakonstantinou
Giorgos Papakonstantinou on 17 Jan 2014
Thank you Azzi! Exactly what I wanted!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!