Calculate the number of nonzero rows
Afficher commentaires plus anciens
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
le 29 Août 2019
B =[3 4;5 0;0 0];
result=sum(~(all(B==0,2)))
Réponse acceptée
Plus de réponses (1)
the cyclist
le 29 Août 2019
Modifié(e) : the cyclist
le 29 Août 2019
2 votes
Use any instead of all. Your code is counting rows in which all entries are non-zero.
1 commentaire
muhammad muda
le 29 Août 2019
Catégories
En savoir plus sur Mathematics dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!