Ending an infinite loop

9 Ansichten (letzte 30 Tage)
Dipesh
Dipesh am 12 Okt. 2012
What I want to do is start with some term I (at time x = 0). Then after a random time (using the command exprnd), this number I will either move up one or move down one. This then becomes the new I and the process starts again untill I = 0. How would I do this?
I was thinking of writing something like this:
"I = 100
x = exprnd(2)
y = 2*(randi([0,1]))- 1
while I = 100
x
I + y
I = I + y
end"
However I don't know how to end this, and therefore I can't graph it or anything to check if it works because it keeps on working out the values for ever. How do I do this?
  6 Kommentare
Azzi Abdelmalek
Azzi Abdelmalek am 12 Okt. 2012
Bearbeitet: Azzi Abdelmalek am 12 Okt. 2012
I+y and what? I+y will be affected to which variable? the same question for x
Azzi Abdelmalek
Azzi Abdelmalek am 12 Okt. 2012
and if you add randomly 1 & -1 you will never close the loop

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Matt Fig
Matt Fig am 12 Okt. 2012
With I initially at 100, you may never get out of the loop. This will go until I gets to the origin (or ctrl+c).
I = 5; % Start at 5
while I
I = I + (-1)^round(rand);
plot(I,0,'o')
xlim([-20 20])
pause(1);
end

Kategorien

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

Community Treasure Hunt

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

Start Hunting!

Translated by