Calculate the number of nonzero rows
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
muhammad muda
am 29 Aug. 2019
Kommentiert: muhammad muda
am 29 Aug. 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 Kommentar
Akzeptierte Antwort
Stephen23
am 29 Aug. 2019
>> nnz(any(B,2))
ans = 2
2 Kommentare
Jos (10584)
am 29 Aug. 2019
the way to go, but if you insist on using ALL, try
nnz(~all(B,2))
Weitere Antworten (1)
the cyclist
am 29 Aug. 2019
Bearbeitet: the cyclist
am 29 Aug. 2019
Use any instead of all. Your code is counting rows in which all entries are non-zero.
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!