필터 지우기
필터 지우기

Conditional statement in random integers to classify

조회 수: 1 (최근 30일)
SeungHyun Cha
SeungHyun Cha 2020년 4월 14일
댓글: Ameer Hamza 2020년 4월 14일
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.

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 4월 14일
편집: Ameer Hamza 2020년 4월 14일
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
SeungHyun Cha
SeungHyun Cha 2020년 4월 14일
I fully understand Thank you a lot.
Thanks
Ameer Hamza
Ameer Hamza 2020년 4월 14일
I am glad to be of help.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

제품

Community Treasure Hunt

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

Start Hunting!

Translated by