# Some basic MATLAB questions

조회 수: 3(최근 30일)
JACK 2022년 6월 14일
댓글: Rena Berman 2022년 7월 19일
Given a vector u of dimension 20x1 consisting of random variables from a uniform-
distribution (distributed on [0,1]).
Instructions:
- Create a code in MATLAB,
- that creates the vector u,
- which contains both 'for loops' and 'if loops',
- and specifies for all values in the vector u in which quarter the number lies.
I managed to create the vector and split it into the quarters. I dont get how to implant the for and if loops.
Can someone help me out ? :((
##### 댓글 수: 4표시숨기기 이전 댓글 수: 3
Rena Berman 2022년 7월 19일

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

### 답변(3개)

KALYAN ACHARJYA 2022년 6월 14일
Hint: rand
https://in.mathworks.com/help/matlab/ref/rand.html
##### 댓글 수: 3표시숨기기 이전 댓글 수: 2
KALYAN ACHARJYA 2022년 6월 14일
@JACK Are you wish to write your own code for quantile function using for loop or if ..any whatever?

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

Torsten 2022년 6월 14일
n = 20;
r = zeros(n,1)
for i=1:n
r(i) = rand;
if r(i) < 0.25
quarter(i) = ...
elseif r(i) >=0.25 && r(i) < 0.5
...
elseif
...
else
...
end
end
Can you take it from here ?
##### 댓글 수: 7표시숨기기 이전 댓글 수: 6
Sam Chak 2022년 6월 14일
@JACK, You don't have to follow exactly. Basically, you create four quarter groups and if the r(i) number satisfies one out of the 4 conditional statements, then that number will be dumped into that group.

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

Walter Roberson 2022년 6월 14일
hint: floor(n*4)
But be careful with the exact boundaries that are multiples of 1/4. For each of those exact values you should write down which bin you want to result, and test your code to make sure it gives those results.
0, ¼, ½, ¾, 1 is five boundaries not 4, and you need to design with that in mind.

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

### 범주

Find more on Matrix Indexing in Help Center and File Exchange

R2022a

### Community Treasure Hunt

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

Start Hunting!

Translated by