0

I would like to have a for loop with 1:25 range but I do not want the for loop to go through number 23 in that range

in another format; I want it like this 1:22 24:25 is it doable this way?

please help

3 Answers 3

6

Yes. You can write:

for num = [1:22 24:25]
    % do something with num
end
Sign up to request clarification or add additional context in comments.

Comments

2

Another solution:

for idx=1:25
    if idx==23, continue, end
    disp(num2str(idx));
end

Comments

2

Just to add an alternative:

skip = [23];
for idx = 1:25
   if ~any(idx == skip)
       %// Your code here
   end
end

I think it's more readable than using [1:22 24:25] as your loop variable as you can see clearly and quickly which numbers are being skipped (unless [1:22 24:25] is a variable getting generated elsewhere in which case I would go with that method), it avoids continue which is controversial and it's easy to add other numbers to skip (i.e. skip = [7, 18, 23] etc...)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.