Setting boundaries of a for loop
Ältere Kommentare anzeigen
Hi, I would like to alter my code so that the random values that I get for position do not excced past a certian point. I would like these values to be no greater than 20 and no smaller than -20 and fit into this range of numbers for 1000 points. How can I write this? Thanks so much, I'm relatively new to Matlab.
position= zeros(1,1000);
position(1)=0;
tails = 0;
heads= 1;
for s=(2:1000)
x= randi([0 1]);
if x==tails
position(s)= position(s-1)-1;
elseif x==heads
position(s)= position(s-1)+1;
end
end
2 Kommentare
madhan ravi
am 26 Nov. 2018
but the random numers that you are generating is 0 and 1?
Estevan Munoz
am 26 Nov. 2018
Antworten (1)
Matt J
am 26 Nov. 2018
position(s) = min(position(s-1)+1 , 20);
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!