# How to find vector with if loops and for loops

3 views (last 30 days)

Show older comments

### Accepted Answer

Image Analyst
on 16 Jun 2022

Are you allowed to turn in other people's solutions as your own for your final exam question?

The first 5 values of the 20-element u lie in the first quarter, the next 5 in the second quarter, the third 5 in the third quarter, and the final 5 elements in indexes 16-20 obviously lie in the last quarter. But I think they want you to use a for loop

u = rand( % You said you got this

for k = 1 : length(u)

if u(k)...........

More code

end

end

You can either store the quarter that the number lives in, in a vector called quarter, or maybe you just want to print out the quarter it's in using fprintf(). Or maybe you want to do both.

##### 15 Comments

Torsten
on 24 Jun 2022

Edited: Torsten
on 24 Jun 2022

I like if the order of the conditions in if-statements can be changed.

This is not the case in this simplified version because

if r(k) < 0.75

quarter(k) = 3;

elseif r(k) < 0.25

quarter(k) = 1;

elseif r(k) < 0.5

quarter(k) = 2;

else

quarter(k) = 4;

end

would create chaos.

### More Answers (0)

### See Also

### Community Treasure Hunt

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

Start Hunting!