Conditional statement in random integers to classify

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.

 Risposta accettata

Ameer Hamza
Ameer Hamza il 14 Apr 2020
Modificato: Ameer Hamza il 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 Commenti

Oh Thank you for replying my question.
But...I must use conditionals and for-loop.
Also, When I used the coding what I uploaded before, it shows only the range25to49.
I have a question about that. I understand I tried to compare vector and scalar which does not makes sense. However, how the range25to49 came out from wrong comparing? (I attached a jpeg file)
By not making sense, I meant that it is not doing what it might appear it does. This expression does have a meaning in MATLAB. If you give a vector as if condition and all the elements are non-zero, the MATLAB considers it as true. For example, try these two codes. First will print false and second will print true
if [1;0;1;1] % vector contains zero
disp('true')
else
disp('false')
end
and the seond code is
if [1;2;3;4] % no zero
disp('true')
else
disp('false')
end
Now, if consider your code, Vector (Vector >= 25 & Vector <= 49) have all non-zero elements so MATLAB consider it true and run
range25to49 = length(find(Vector>=25 & Vector<=49))
If you must use for loop then iterate on each element one by one.
Vector = randi([0 99],10000,1);
range0to24 = 0;
range25to49 = 0;
range50to74 = 0;
range75to99 = 0;
for i = 1:numel(Vector)
if Vector (Vector(i) >= 0 && Vector(i) <= 24)
range0to24 = range0to24 + 1;
elseif Vector (Vector(i) >= 25 & Vector(i) <= 49)
range25to49 = range25to49 + 1;
elseif Vector (Vector(i) >= 50 && Vector(i) <= 74)
range50to74 = range50to74 + 1;
elseif Vector (Vector(i) >= 75 & Vector(i) <= 99)
range75to99 = range75to99 + 1;
end
end
Let me interpret your codes.
Vector = randi([0 99],10000,1);
range0to24 = 0;
range25to49 = 0;
range50to74 = 0;
range75to99 = 0;
Making a column vector including10000 random integers range from 0 to 99, and initial number of each range is 0.
for i = 1:numel(Vector)
i is a raw vector from 1 to 10000 orderly, like [1 2 3 ``` 10000]
if Vector (Vector(i) >= 0 && Vector(i) <= 24)
range0to24 = range0to24 + 1;
Vector(i) is a column vector that includes randomly dispersed intergers.
and If a random integer is in the range from 0 to 24.
the value range 0 to 24 is added by one untill there is no more integers between 0 to 24.
Are they correct?
Ameer Hamza
Ameer Hamza il 14 Apr 2020
Modificato: Ameer Hamza il 14 Apr 2020
"i is a raw vector from 1 to 10000 orderly, like [1 2 3 ``` 10000]"
No. i will not have a vector value. i is just a scalar. The for-loop will run 10000 times, and in each iteration, the value of i will change. In the first iteration it will be 1, in the second iteration it will be 2, and so on.
"Vector(i) is a column vector that includes randomly dispersed intergers."
Vector(i) is not a column vector. It is the i-th element of the column vector, i.e., it is just a scalar number. In each iteration, the Vector(i) will take one element of Vector and find out in which range it lies. Finding the correct range, it increments the counter.
I fully understand Thank you a lot.
Thanks
I am glad to be of help.

Accedi per commentare.

Più risposte (0)

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by