Ending an infinite loop

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

I = 100 x I + y I = I + y ? what does that mean
Dipesh
Dipesh 2012년 10월 12일
Fixed it
Azzi Abdelmalek
Azzi Abdelmalek 2012년 10월 12일
편집: Azzi Abdelmalek 2012년 10월 12일
for which value of I do you want to stop it? and what means
x
I+y
Dipesh
Dipesh 2012년 10월 12일
I want it to stop when I = 0. I is going to be on 'y' axis. x is a exponential random variable with mean 2. y is a random 1 or -1 so I thought by doing I + y, I will add 1 or take 1 away from the graph. It makes sense as a stairstep plot. As x "time" goes accross, the graph either randomley goes up 1 or down 1. This keeps happening until I = 0.
Azzi Abdelmalek
Azzi Abdelmalek 2012년 10월 12일
편집: Azzi Abdelmalek 2012년 10월 12일
I+y and what? I+y will be affected to which variable? the same question for x
Azzi Abdelmalek
Azzi Abdelmalek 2012년 10월 12일
and if you add randomly 1 & -1 you will never close the loop

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

답변 (1개)

Matt Fig
Matt Fig 2012년 10월 12일

0 개 추천

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

카테고리

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

질문:

2012년 10월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by