Conditional statement in random integers to classify

1 visualización (últimos 30 días)
SeungHyun Cha
SeungHyun Cha el 14 de Abr. de 2020
Comentada: Ameer Hamza el 14 de Abr. de 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.

Respuesta aceptada

Ameer Hamza
Ameer Hamza el 14 de Abr. de 2020
Editada: Ameer Hamza el 14 de Abr. de 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 comentarios
SeungHyun Cha
SeungHyun Cha el 14 de Abr. de 2020
I fully understand Thank you a lot.
Thanks
Ameer Hamza
Ameer Hamza el 14 de Abr. de 2020
I am glad to be of help.

Iniciar sesión para comentar.

Más respuestas (0)

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by