Effacer les filtres
Effacer les filtres

Calculate the number of nonzero rows

3 vues (au cours des 30 derniers jours)
muhammad muda
muhammad muda le 29 Août 2019
Commenté : muhammad muda le 29 Août 2019
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 commentaire
KALYAN ACHARJYA
KALYAN ACHARJYA le 29 Août 2019
B =[3 4;5 0;0 0];
result=sum(~(all(B==0,2)))

Connectez-vous pour commenter.

Réponse acceptée

Stephen23
Stephen23 le 29 Août 2019
>> nnz(any(B,2))
ans = 2
  2 commentaires
Jos (10584)
Jos (10584) le 29 Août 2019
the way to go, but if you insist on using ALL, try
nnz(~all(B,2))
muhammad muda
muhammad muda le 29 Août 2019
yes it works! Thanks!

Connectez-vous pour commenter.

Plus de réponses (1)

the cyclist
the cyclist le 29 Août 2019
Modifié(e) : the cyclist le 29 Août 2019
Use any instead of all. Your code is counting rows in which all entries are non-zero.
  1 commentaire
muhammad muda
muhammad muda le 29 Août 2019
Thanks, i'll use any then :)

Connectez-vous pour commenter.

Catégories

En savoir plus sur Sparse Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by