Overwriting While-loop conditional with +Inf

조회 수: 1 (최근 30일)
Douglas Bock
Douglas Bock 2017년 4월 20일
편집: Andrew Newell 2017년 4월 21일
In my code, I have this loop:
while a~=1 && b <= totCycles
...
end
I want the loop to run as long as the conditions hold, obviously.
Sometimes, however, I do not want the second condition. I want to effectively make it always true.
Setting totCycles = +Inf looke like the right way to do this, but it doesnt seem to work!
Any ideas why this might be? Or any other workaround?
Many Thanks
Doug
  댓글 수: 3
Douglas Bock
Douglas Bock 2017년 4월 21일
The loop keeps iterating, even if the first condition is no longer met.
Stephen23
Stephen23 2017년 4월 21일
@Douglas Bock: how do you know that the first condition is not met? How did you check this? Are the two values integer or floating point?

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

답변 (2개)

Andrew Newell
Andrew Newell 2017년 4월 21일
편집: Andrew Newell 2017년 4월 21일
MATLAB evaluates a logical expression like your while condition from left to right. If a==1, it knows the expression is false and does not even look at the second part. So I suspect that a is a floating point number, and when you think it is equal to 1 it is not exactly equal to 1. See this FAQ.

hmi amid
hmi amid 2017년 4월 21일
You can try this :
while a~=1 && (b <= totCycles || idontcare_b)
...
idontcare_b = true % in a place you dont care about the value of b
end

카테고리

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