필터 지우기
필터 지우기

Cutting a for loop when a certain value is repeated

조회 수: 9 (최근 30일)
Jed
Jed 2014년 4월 16일
댓글: Jed 2014년 4월 16일
Is there any way of cutting a for loop when a certain value is reached? I have a loop in the form of
for k = 1:numel(filelist)
this loop produces a value of X at the end of every loop which is then stored in a text file. I was just wondering whether there is any way that the for loop can be cut when this value of X stops increasing/ stays the same?
I thought something maybe along the lines of
if X(k-1)==X(k)
break
end
but this doesn't work. I guess its because the loop overwrites X. I was just wondering whether anyone had any ideas? Thanks in advance!

답변 (2개)

Azzi Abdelmalek
Azzi Abdelmalek 2014년 4월 16일
It's better to use a while loop
while x(k-1)~=x(k)
% your code
end
  댓글 수: 1
Jed
Jed 2014년 4월 16일
편집: Jed 2014년 4월 16일
sorry i forgot to mention that there are multiple 0 values at the very beginning. It is for a spray injection where there are multiple images showing zero spray, spray then starts and then i wanted it to cut-off when it stops increasing. And also how does that work when x hasn't been defined yet? as it is defined at the end of the loop

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


Sean de Wolski
Sean de Wolski 2014년 4월 16일
Sure, pseudocode:
xtemp = nan;
for ii = 1:1000
x = whatever
do stuff
if x==xtemp
break
else
xtemp = x;
end
end
  댓글 수: 4
Jed
Jed 2014년 4월 16일
Aw thank you! that makes much more sense now. In this case though there are either 4 of 5 blank images at the beginning where x=0 which would break this loop too early. Is there any way to get around this?
Jed
Jed 2014년 4월 16일
i tried putting that within its own while loop, saying:
while X>0
if X==Xtemp
break
else
Xtemp = X;
end
end
but that didnt work, it completely ignored the whole statement

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

카테고리

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