Count the number of non-zero elements of a column/row of a 2D-Matrix?
80 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hello everyone,
I have a 2D Matrix and I want to make Matlab count the number of non-zero Elements within one row, is there a straightforward way to do this? Many thanks
Akzeptierte Antwort
Weitere Antworten (2)
Thomas
am 3 Mai 2012
Try:
a=[3 0 0;0 2 0; 1 0 4] %input matrix
sum(a~=0,2) % no. of nozero elements in each row
0 Kommentare
Muhammad Ilias Amin
am 12 Mai 2014
a = [3 0 0; 0 2 0; 1 0 0];
To get the no of non zero columns: sum( all( a == 0, 1 ) );
To get the no of non zero rows: sum( all( a == 0, 2 ) );
1 Kommentar
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!