Effacer les filtres
Effacer les filtres

comparison/checking without for loop

1 vue (au cours des 30 derniers jours)
Ricky
Ricky le 4 Mai 2012
Hi there,
Say if I have a random event with just -1 and 1. e.g. a = randsrc(5,1);%that will generate a 5x1 matrix with just -1 or 1
Now say if I want to check how many of -1 or 1 in a, how can I do that without using for loop? I try to do this but doesn't work:
m =0;
if(a == 1)
m = m+1;
end
Any help would be really appreciated.
Cheers,
Rak
  1 commentaire
Andrei Bobrov
Andrei Bobrov le 4 Mai 2012
accumarray((a > 0) + 1,a)

Connectez-vous pour commenter.

Réponse acceptée

Geoff
Geoff le 4 Mai 2012
nneg = sum(a==-1);
npos = sum(a==1);
  5 commentaires
Geoff
Geoff le 4 Mai 2012
Yes... replace '0' and '42' in my example with 't1' and 't0' respectively.
Ricky
Ricky le 4 Mai 2012
Thanks Geoff!!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Produits

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by