run a while loop while excluding some point

9 次查看(过去 30 天)
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.

采纳的回答

Geoff Hayes
Geoff Hayes 2015-4-19
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 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by