How to check if there are more ones than zeros in the variable.
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Luccas S.
le 22 Juil 2022
Réponse apportée : Image Analyst
le 22 Juil 2022
For example, if there is the variable x;
x = [1 1 1 1 1 1 1 1 0 0 0];
How to check if it has ones than zeros?
1 commentaire
Mohammad Sami
le 22 Juil 2022
if the verctor only contains 1s and 0s, you can just do a sum divided by length of the vector
Réponse acceptée
Walter Roberson
le 22 Juil 2022
x = [1 1 1 1 1 1 1 1 0 0 0]
has_more_ones = mean(x) > 0.5 %note that exactly equal does not qualify as "more"
0 commentaires
Plus de réponses (1)
Image Analyst
le 22 Juil 2022
Another way (assuming only 1s and 0s in the array):
moreOnes = nnz(x) > numel(x)/2
0 commentaires
Voir également
Catégories
En savoir plus sur Entering Commands 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!