Calculate the number of nonzero rows
6 ビュー (過去 30 日間)
古いコメントを表示
Hi,
How can I calculate the number of nonzero rows in a matrix?
I have
B =
3 4
5 0
0 0
and the number of nonzero rows (that I need) is 2. (because the 2nd row has the element of '5')
I tried sum( all( B ~= 0, 2 ) ); but the answer is 1.
採用された回答
Stephen23
2019 年 8 月 29 日
>> nnz(any(B,2))
ans = 2
2 件のコメント
Jos (10584)
2019 年 8 月 29 日
the way to go, but if you insist on using ALL, try
nnz(~all(B,2))
その他の回答 (1 件)
the cyclist
2019 年 8 月 29 日
編集済み: the cyclist
2019 年 8 月 29 日
Use any instead of all. Your code is counting rows in which all entries are non-zero.
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!