Filter löschen
Filter löschen

Conditional statement in random integers to classify

1 Ansicht (letzte 30 Tage)
SeungHyun Cha
SeungHyun Cha am 14 Apr. 2020
Kommentiert: Ameer Hamza am 14 Apr. 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.

Akzeptierte Antwort

Ameer Hamza
Ameer Hamza am 14 Apr. 2020
Bearbeitet: Ameer Hamza am 14 Apr. 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 Kommentare
SeungHyun Cha
SeungHyun Cha am 14 Apr. 2020
I fully understand Thank you a lot.
Thanks
Ameer Hamza
Ameer Hamza am 14 Apr. 2020
I am glad to be of help.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by