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

mat(sum(mat==0,2)==2,:)
For general case
N=2
out=mat(sum(mat==0,2)==N,:)

3 Comments

Thank you Azzi but this does not give me the rows which have 2 zeros. This gives me the rows which no zeros.
Look at edited answer
Thank you Azzi! Exactly what I wanted!

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!