In Matlab, I am performing calculations during a for loop but am trying to find a way to skip iterations during the for loop if certain criteria are met. I have written out a quick example to illustrate my question.
In the code below, the for loop will go through iterations 1 and 2 and output as expected into r. r(1) will be 1, and r(2) will be 2. Once the for loop runs through iteration 3, the value of 20 will be placed into r(3). After this takes place, then I want the for loop to skip the next 5 iterations and go straight to iteration 8 of the for loop.
for i=1:1:10
if i==3
r(i)=20;
i = i+5;
else
r(i) = i;
end
end
The actual result for r is as follows:
r =
1 2 20 4 5 6 7 8 9 10
However, I would like for the result to appear similar to the following. (PLEASE NOTE that I am not looking to fill the desired r(4):r(7) with 0 but rather looking to skip for loop iterations 4 through 7 entirely.)
r =
1 2 20 0 0 0 0 8 9 10
If anyone has advice, that will be greatly appreciated. Thank you!