Hi, is it possible to count the number of ones in a matrix ?
242 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
nafila aytija
am 8 Apr. 2016
Kommentiert: Walter Roberson
am 21 Jun. 2021
Example: Suppose we a have a matrix A=[1 0 0 1 0 0; 1 0 0 0 1 1]; how do I count the number of ones in this matrix??
0 Kommentare
Akzeptierte Antwort
Ahmet Cecen
am 8 Apr. 2016
Bearbeitet: Ahmet Cecen
am 8 Apr. 2016
sum(A(:) == 1);
6 Kommentare
Walter Roberson
am 21 Jun. 2021
A = randi([0 1], 5, 5); A = xor(A,A.'); %random symmetric
A
[r,c] = find(A);
mask = r>=c;
r = r(mask); c = c(mask);
[r,c]
or
[r,c] = find(tril(A))
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Matrices and Arrays 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!