Some basic MATLAB questions

1 visualización (últimos 30 días)
JACK
JACK el 14 de Jun. de 2022
Comentada: Rena Berman el 19 de Jul. de 2022
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 comentarios
Sam Chak
Sam Chak el 14 de Jun. de 2022
Hey @JACK, if YES exactly, please edit your question for clarity so that you get meaningful Answers.
Rena Berman
Rena Berman el 19 de Jul. de 2022
(Answers Dev) Restored edit

Iniciar sesión para comentar.

Respuestas (3)

KALYAN ACHARJYA
KALYAN ACHARJYA el 14 de Jun. de 2022
Hint: rand
https://in.mathworks.com/help/matlab/ref/rand.html
  3 comentarios
Steven Lord
Steven Lord el 14 de Jun. de 2022
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 el 14 de Jun. de 2022
@JACK Are you wish to write your own code for quantile function using for loop or if ..any whatever?

Iniciar sesión para comentar.


Torsten
Torsten el 14 de Jun. de 2022
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 comentarios
JACK
JACK el 14 de Jun. de 2022
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 el 14 de Jun. de 2022
@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.

Iniciar sesión para comentar.


Walter Roberson
Walter Roberson el 14 de Jun. de 2022
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.

Categorías

Más información sobre Programming en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by