Loop order/ assignement
조회 수: 1 (최근 30일)
이전 댓글 표시
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??
댓글 수: 0
답변 (1개)
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
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 Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!