Some basic MATLAB questions

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

Torsten
Torsten 2022년 6월 14일
Unclear.
What are and why "if loops" ?
What do you mean by "quarter" ? You mean if the numbers lie in [0:0.25],(0.25:0.5],(0.5:0.75] or (0.75:1] ?
JACK
JACK 2022년 6월 14일
편집: JACK 2022년 6월 14일
Yes exacly, i can't figure out how to manage to get the numbers into those quarters. I asume that the question wants that i arange the given random number into those intervals
Sam Chak
Sam Chak 2022년 6월 14일
Hey @JACK, if YES exactly, please edit your question for clarity so that you get meaningful Answers.
Rena Berman
Rena Berman 2022년 7월 19일
(Answers Dev) Restored edit

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

답변 (3개)

KALYAN ACHARJYA
KALYAN ACHARJYA 2022년 6월 14일

0 개 추천

Hint: rand
https://in.mathworks.com/help/matlab/ref/rand.html

댓글 수: 3

JACK
JACK 2022년 6월 14일
Thanks !!! , may i get a hint relatet how to manage to use the for/if loops.
u = rand (20,1)
quantile(u,[0.2,0.4,0.6,0.8])
I worte smth like this to get some first results
There's no such thing as an "if loop", but as for a for loop how would you solve the problem if the quantile function only accepted a scalar (one number, not a vector) as its first input?
Once you've written that code, replace the call to the quantile function with an if / elseif / else / end block.
% Save this as countVonCount.m :)
x = 3;
if x == 1
disp("One!")
elseif x == 2
disp("Two!")
elseif x == 3
disp("Three!")
else
disp("Ah ah ah!")
end
Three!
KALYAN ACHARJYA
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
Torsten 2022년 6월 14일

0 개 추천

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

Image Analyst
Image Analyst 2022년 6월 14일
Will you get in trouble if you turn in @Torsten's solution as your own and your professor finds out? Would you face disciplinary action for cheating?
Torsten
Torsten 2022년 6월 14일
That's the sceleton - you must add the flesh.
Sam Chak
Sam Chak 2022년 6월 14일
@JACK, Your QUESTION has DISAPPEARED!!!
JACK
JACK 2022년 6월 14일
편집: JACK 2022년 6월 14일
@Image Analyst Well i don't know, im omw to gather some information. I will not turn in the solution of Torsten's.
JACK
JACK 2022년 6월 14일
편집: JACK 2022년 6월 14일
@Torsten hey Torsten well i added the flesh. The only part that i don't get is quarter(i) = ... part. I managed to fill the other parts but cant define what to add to the ... seciton :S
JACK
JACK 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
r(i) >=0.5 && r(i) < 0.75
else
r(i) >=0.75 && r(i) < 1
end
end
Sam Chak
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
Walter Roberson 2022년 6월 14일

0 개 추천

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.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

릴리스

R2022a

태그

질문:

2022년 6월 14일

댓글:

2022년 7월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by