フィルターのクリア

Calculate the number of nonzero rows

5 ビュー (過去 30 日間)
muhammad muda
muhammad muda 2019 年 8 月 29 日
コメント済み: muhammad muda 2019 年 8 月 29 日
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.
  1 件のコメント
KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 8 月 29 日
B =[3 4;5 0;0 0];
result=sum(~(all(B==0,2)))

サインインしてコメントする。

採用された回答

Stephen23
Stephen23 2019 年 8 月 29 日
>> nnz(any(B,2))
ans = 2
  2 件のコメント
Jos (10584)
Jos (10584) 2019 年 8 月 29 日
the way to go, but if you insist on using ALL, try
nnz(~all(B,2))
muhammad muda
muhammad muda 2019 年 8 月 29 日
yes it works! Thanks!

サインインしてコメントする。

その他の回答 (1 件)

the cyclist
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.
  1 件のコメント
muhammad muda
muhammad muda 2019 年 8 月 29 日
Thanks, i'll use any then :)

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSparse Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by