필터 지우기
필터 지우기

Calculate the number of nonzero rows

조회 수: 3 (최근 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.

채택된 답변

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.

카테고리

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