Effacer les filtres
Effacer les filtres

Conditional statement in random integers to classify

1 vue (au cours des 30 derniers jours)
SeungHyun Cha
SeungHyun Cha le 14 Avr 2020
Commenté : Ameer Hamza le 14 Avr 2020
for Vector = randi([0 99],10000,1)
if Vector (Vector >= 0 & Vector <= 24)
range0to24 = length(find(Vector>=0 & Vector<=24))
elseif Vector (Vector >= 25 & Vector <= 49)
range25to49 = length(find(Vector>=25 & Vector<=49))
elseif Vector (Vector >= 50 & Vector <= 74)
range50to74 = length(find(Vector>=50 & Vector<=74))
elseif Vector (Vector >= 75 & Vector <= 99);
range75to99 = length(find(Vector>=75 & Vector<=99))
end
end
I want to make conditional statements that classify random integer numbers in the range of 0 to 99.
I think actually, Vector is a 10000 X 1 column vector, so I think above codes should be worked.
However, it shows only range25to49 even though, there is no difference between other range codes.
I do not understand why other ranges are not shown. Please help me ㅠㅠ
P.S. I must use only for-loop and conditional statements.

Réponse acceptée

Ameer Hamza
Ameer Hamza le 14 Avr 2020
Modifié(e) : Ameer Hamza le 14 Avr 2020
Here is the simplified code
Vector = randi([0 99],10000,1);
range0to24 = sum(Vector >= 0 & Vector <= 24);
range25to49 = sum(Vector >= 25 & Vector <= 49);
range50to74 = sum(Vector >= 50 & Vector <= 74);
range75to99 = sum(Vector >= 75 & Vector <= 99);
You code is not working for several reasons. For example, in the line
if Vector (Vector >= 0 & Vector <= 24)
the Vector is a 10000x1 vector and you are comparing it with scalar. This does not make much sense in MATLAB. Also,
length(find(Vector>=75 & Vector<=99))
using length, and find together is redundant. You can simply sum the logical array.
  6 commentaires
SeungHyun Cha
SeungHyun Cha le 14 Avr 2020
I fully understand Thank you a lot.
Thanks
Ameer Hamza
Ameer Hamza le 14 Avr 2020
I am glad to be of help.

Connectez-vous pour commenter.

Plus de réponses (0)

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by