필터 지우기
필터 지우기

How to set stopping condition on time in nested for loop?

조회 수: 1 (최근 30일)
Noor Fatima
Noor Fatima 2022년 10월 21일
댓글: Noor Fatima 2022년 10월 21일
tic
Time = 0;
flag = 0;
for kk = 1: n
some operations
for ii = 1:m
if Time >= Time_limit
flag = 1;
break
end
some operations
end
Time = toc;
if(flag==1)
break
end
end
But the above code does not stop when a time limit reached.

채택된 답변

Walter Roberson
Walter Roberson 2022년 10월 21일
tic
Time = 0;
for kk = 1: n
some operations
for ii = 1:m
Time = toc; %you just did some work, you might have reached the limit
if Time >= Time_limit; break; end
some operations
end
Time = toc; %you just did some work, you might have reached the limit
if Time >= Time_limit; break; end
end
The second call to toc is because you might have done some work when ii == m that put you over the time limit. There is no toc just before the end of the for ii loop so in the case where you left the for loop because you ran out of iterations, you have not updated the time.

추가 답변 (1개)

David Hill
David Hill 2022년 10월 21일
tStart = cputime;
c=0;
while 1
if cputime-tStart>20
break
end
c=c+1;
end
c
c = 23672107
  댓글 수: 3
Noor Fatima
Noor Fatima 2022년 10월 21일
Based on elapsed time
Noor Fatima
Noor Fatima 2022년 10월 21일
@David Hill Thank you for your answer but I want to find elapsed time with in double for loop.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by