run a while loop while excluding some point

7 Ansichten (letzte 30 Tage)
sujit
sujit am 19 Apr. 2015
Beantwortet: Geoff Hayes am 19 Apr. 2015
This is a pretty basic but silly doubt i have.
Suppose, i have a while loop in my code, something like this:
f=1;
while f<365
% some functions with variable f.
f=f+1;
end;
So, ideally the while loop will run for values of variable 'f' from 1 to 365.
Now, i don't want to run the loop for variable values f= 15,16,17,300,...(some 65 values).
How can i employ this in my code?
Thanks.

Akzeptierte Antwort

Geoff Hayes
Geoff Hayes am 19 Apr. 2015
Sujit - create an array of those values of f that you wish to ignore and then use that array in your while loop to decide whether to rvalue the functions for f. For example,
ignoreVals = [15 16 17 300];
f = 1;
while f < 365
if ~ismember(f, ignoreVals)
% some functions with variable f
end
f = f + 1;
end
In the above code, we use the ismember function to determine whether f is a member of the array of values that we wish to ignore. If not, then we evaluate some functions for that f.
Try the above and see what happens!

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements 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!

Translated by