Calculate the number of nonzero rows

14 次查看(过去 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
Stephen23 2019-8-29
>> nnz(any(B,2))
ans = 2

更多回答(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.

类别

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

标签

Community Treasure Hunt

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

Start Hunting!

Translated by