Break the for loop

조회 수: 2 (최근 30일)
Julenissen
Julenissen 2021년 10월 26일
댓글: Mathieu NOE 2021년 10월 27일
Hi! I have a for loop that fills my vector with fibonacci numbers if random number from 0 to 1 is greater than 0,1. In cases when random number is less than 0,1, instead of adding the leading digits, it subtracts. So normally result looks like this:
1
1
2
3
5
8
13
5
18
23
But in some cases random number from 0 to 1 gives value less than 0,1 twice in a row and i get a table with fibonacci number <= 0 that looks like this:
1
1
2
3
1
-2
-1
-3
-4
-7
So i have two questions that i have not managed to solve.
  1. How to stop the loop when i get first negative value (in this case -2) and write a message about that?
  2. How to make this for loop automaticly go over and over again (implement it in while loop?) until i get a negative value and than stop it and then write this message? Is it possible to count how many times (while?) loop had to start before negative value was found?
n = 10;
limitf = 0.1;
F = zeros(n,1);
F(1) = 1;
F(2) = 1;
for k = 3:n
r = rand(1);
if r > limitf
F(k) = F(k-1) + F(k-2);
else
F(k) = F(k-1) - F(k-2);
end
end
disp(F)

채택된 답변

Mathieu NOE
Mathieu NOE 2021년 10월 26일
hello
I modified a bit your code so the condition were the two successive identical random r :
n = 10;
limitf = 0.1;
F = zeros(n,1);
F(1) = 1;
F(2) = 1;
r_old = -1;
for k = 3:n
r_old = r;
r = rand(1);
if r > limitf
F(k) = F(k-1) + F(k-2);
else
if abs(r-r_old)>eps
F(k) = F(k-1) - F(k-2);
end
end
end
disp(F)
  댓글 수: 6
Julenissen
Julenissen 2021년 10월 27일
Thank you :)
Mathieu NOE
Mathieu NOE 2021년 10월 27일
My pleasure !

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

추가 답변 (1개)

Alan Stevens
Alan Stevens 2021년 10월 26일
Something like this perhaps:
fib = [1; 1];
its = 0;
keepgoing = true;
while keepgoing
its = its+1;
r = rand;
if r>0.1
fib(its+2) = fib(its+1) + fib(its);
else
fib(its+2) = fib(its+1) - fib(its);
end
if fib(its+2)<0
keepgoing = false;
end
end
disp(['number of iterations ' num2str(its)])
  댓글 수: 4
Alan Stevens
Alan Stevens 2021년 10월 26일
The following restircts your vetor to 10 elements. Not clear to me if you want to start from 1 1 each time the 10 values are exceeded, or if you just want to carry on with the larger Fibonacci numbers. The following does the latter. It answers the first question automatically (the message I've printed just indicates the number of iterations. Replace with whatever message you want.)
fib = [1; 1];
its = 0;
keepgoing = true;
p1 = 1; p2 = 2; p3 = 3;
while keepgoing
its = its+1;
if p1>10
p1 = 1;
end
if p2 > 10
p2 = 1;
end
if p3 > 10
p3 = 1;
end
r = rand;
if r>0.1
fib(p3) = fib(p2) + fib(p1);
else
fib(p3) = fib(p2) - fib(p1);
end
if fib(p3)<0
keepgoing = false;
end
p1 = p1+1;
p2 = p2+1;
p3 = p3+1;
end
disp(['number of iterations ' num2str(its)])
Julenissen
Julenissen 2021년 10월 26일
The next loop have to start from 1 and 1 again :)

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

카테고리

Help CenterFile Exchange에서 Calculus에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by