derive new variable with random float values using for loop

3 Ansichten (letzte 30 Tage)
Hi, I am trying to derive a new variable with random float values using for loop. Tried using randn.
for i=1:length(hosp_yn_coded)
if (hosp_yn_coded(i)==1)
los_total(i)=randn([0 2],1,1) % must be float bet 0-2 e.g. 0.1,0.2,0.4,1.4,1.2
else
los_total(i)=randn([0 1],1,1) % must be float bet 0-2 e.g. 0.1,0.2,0.4,1.4,1.2
end
end
Thanks so much!

Akzeptierte Antwort

Jeff Miller
Jeff Miller am 13 Dez. 2020
Despite your comments, I guess you want 0-1 for the second option, not 0-2.
I guess you want all numbers in the range to be equally likely, so randn is not the right choice.
I wouldn't use a for loop here, but you can if you want:
for i=1:length(hosp_yn_coded)
if (hosp_yn_coded(i)==1)
los_total(i)=2*rand; % rand gives 0-1, so 2*rand is 0-2
else
los_total(i)=rand;
end
end
  1 Kommentar
Arturo Jr. Ongkeko
Arturo Jr. Ongkeko am 13 Dez. 2020
Hi, Jeff! Thanks so much. I am trying to be comfortable with for-loops so this is like a practice for me.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by