Find rows containing N zeros in a matrix

2 次查看(过去 30 天)
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

采纳的回答

Azzi Abdelmalek
Azzi Abdelmalek 2014-1-16
编辑:Azzi Abdelmalek 2014-1-17
mat(sum(mat==0,2)==2,:)
For general case
N=2
out=mat(sum(mat==0,2)==N,:)

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by