Calculate the number of nonzero rows
조회 수: 3 (최근 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
추가 답변 (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.
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!