Overwriting While-loop conditional with +Inf
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Stephen23
am 21 Apr. 2017
@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?
Antworten (2)
Andrew Newell
am 21 Apr. 2017
Bearbeitet: Andrew Newell
am 21 Apr. 2017
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.
0 Kommentare
hmi amid
am 21 Apr. 2017
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
0 Kommentare
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!