if statement to skip certain values in loop
if statement to skip certain values in loop
I have a range of X and Y values, and I have a domain that I don't want to calculate the values of nodes within. I want to have an if statement within a loop in Matlab to skip these.
X
Y
if
For example:
X = [1:20]
Y = [1:20]
X = [5:7]
Y = [12:14]
I think the code should be something like this:
for X=1:20
for Y=1:20
if X=2:7 & Y=12:14
return
end
% the operation here !
T(X,Y) = lab lab lab
end
end
However, I'm not sure how to properly write the condition.
1 Answer
1
You could use ismember or a combination of any and == for the condition, and continue is the command for skipping to the next loop index...
ismember
any
==
continue
% ismember example
for X = 1:20
for Y = 1:20
if ismember( X, 2:7 ) && ismember( Y, 12:14 )
continue
end
% your loop operations
end
end
In this case, you could replace the if condition with
if
if any( X == 2:7 ) && any( Y == 12:14 )
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments
Post a Comment