Find rows containing N zeros in a matrix
2 views (last 30 days)
Show older comments
Giorgos Papakonstantinou
on 16 Jan 2014
Commented: Giorgos Papakonstantinou
on 17 Jan 2014
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
0 Comments
Accepted Answer
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
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!