run a while loop while excluding some point
2 views (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
Geoff Hayes
on 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!
0 Comments
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!