필터 지우기
필터 지우기

Loop order/ assignement

조회 수: 1 (최근 30일)
Amine Ben Ayara
Amine Ben Ayara 2016년 1월 15일
댓글: Walter Roberson 2016년 1월 15일
Hello,
I am writing a loop say : for i=1:9;
Suppose I am in the 5th iteration
if certain conditions of my If else statement are met, then I want my next loop to start from iteration nbr5 not from i=6.
82 4.405517686 6.551066224 8.37093675 9.377805584
70 3.549776369 5.46044706 7.393870673 8.899016011
41 3.925365061 6.109160353 8.117877657 9.186700016
85 3.258680565 5.152732852 7.091023754 8.418727381
99 4.047440897 6.097040296 7.774985958 8.495417896 ( 5th row: i=5).
so in i+1= 6, I want my loop to actually still start from i=5 ( 5th row),
is this even possible in Matlab?
can someone PLEASE help??

답변 (1개)

Walter Roberson
Walter Roberson 2016년 1월 15일
No. You will need to switch from using a "for" loop to using a "while" loop.
(There are also some ways of writing it that involve putting a while loop within the for loop.)
  댓글 수: 2
Amine Ben Ayara
Amine Ben Ayara 2016년 1월 15일
Hello Walter, Thank you for your reply. Just just to clarify, If I write for loop: for i=1:6 or 7, the while loop goes it right below it? for i=1:6
Walter Roberson
Walter Roberson 2016년 1월 15일
Two forms:
i = 1;
while i <= 9
... do something ...
if conditions are *not* met
i = i + 1;
end
end
or
for i = 1 : 9
loop_again = false;
while loop_again
... do something ...
if conditions are met
loop_again = true;
end
end
end
The first of those obviously takes less code. It also has the possibility of going backwards by subtracting from i.

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by