For loop with two conditions
Ältere Kommentare anzeigen
Hi,
I'm trying to run this code where it should stop as soon as one of the conditions has met and give me the index where it stops.
So, here is my code and it's taking so long to run and it also didn't stop utill the end which I'm sure one of those conditions should have been met far away from the end.
isOK1= (Discard2value<=target_value_30_P);
isOK2 =(Discard2value>=t2);
for N = datan(1:end)
while 1
if N == (Discard1value) || ((isOK1 && isOK2))
J = N;
break
end
end
end
I don't know what's missing? Any help would be appreciate it.
Also, I keep getting this error : "array indices must be positive integers or logical values"
Thanks in advance.
Akzeptierte Antwort
Weitere Antworten (1)
Paul Hoffrichter
am 5 Jul. 2021
>> this code where it should stop as soon as one of the conditions has met
But the break just gets you out of the while loop when one of the two conditions is met. You need to add a test after the while loop to see if you broke out, in which case you can add another break do get out of the for-loop.
But within the while loop, here are your constants:
- N (is being tested, but not changing)
- Discard1value (is being tested, but not changing)
- isOK1, isOK2 (are being tested but not changing)
Within the for-loop, only one variable N changes.
If the first time you hit the if N... test, you have a false condition, then you will never break out - you will be in an infinite loop since all the variables in the if test are constant within the body of the if-statement.
isOK1= (Discard2value<=target_value_30_P);
isOK2 =(Discard2value>=t2);
for N = datan(1:end)
while 1
if N == (Discard1value) || ((isOK1 && isOK2))
J = N;
break
end
end % END while
end % END for
>> it's taking so long to run and it also didn't stop utill the end
Since you are not reporting an infinite loop, then one of the conditions in the if-test must be true the first time you hit it. If the isOK test true, then it remains true for all values of N.
I advise you to put a breakpoint at the if-test, and step through a few loops to better understand your two loops.
4 Kommentare
Saad Alqahtani
am 6 Jul. 2021
Paul Hoffrichter
am 6 Jul. 2021
Bearbeitet: Paul Hoffrichter
am 6 Jul. 2021
But you have not changed your for- and while-loop bodies. The constants are still constants from your original posting. Only N is changing.
And you did not modify the break handling as discussed in order to get out of the for-loop immediately when the while-loop breaks.
for N = datan(1:end)
while 1
if N == (Discard1value) || ((isOK1 && isOK2))
J = N;
break
end
end
end
BTW, if you do CtrlA followed by Ctrl/I, you will get better indentation.
Image Analyst
am 6 Jul. 2021
This would be some more robust code, which breaks out of both the for loop and the while loop as well as having a failsafe to prevent an infinite loop
isOK1= (Discard2value<=target_value_30_P);
isOK2 =(Discard2value>=t2);
abort = false;
loopCounter = 1; % Failsafe
maxIterations = 1000; % A lot more than you EVER expect to have.
for N = datan(1:end)
while ~abort && loopCounter < maxIterations
if N == (Discard1value) || ((isOK1 && isOK2))
J = N;
abort = true;
break; % out of while loop
end
% Now update conditions N, Discard1value, isOK1, and/or isOK2 somehow
% or else it will never hit the break.
% TODO : update code, then increment the loop counter.
loopCounter = loopCounter + 1; % Failsafe
end
% You get here when the while loop breaks OR
% if you hit the max number of iterations.
if abort
break; % Out of for loop.
end
end
Saad Alqahtani
am 6 Jul. 2021
Kategorien
Mehr zu Function Creation finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!