Hi, is it possible to count the number of ones in a matrix ?
242 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
nafila aytija
le 8 Avr 2016
Commenté : Walter Roberson
le 21 Juin 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 commentaires
Réponse acceptée
Ahmet Cecen
le 8 Avr 2016
Modifié(e) : Ahmet Cecen
le 8 Avr 2016
sum(A(:) == 1);
6 commentaires
Walter Roberson
le 21 Juin 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))
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Matrices and Arrays 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!