7

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!

3 Answers3

7

Use a while loop instead of a for loop to increment it manually:

i=1;  // index for loop
k=1;  // index for r
r = zeros(1,10) // pre-allocate/cut is faster
while i <= 10
  if i == 3
    r(i)=20;
    i = i+5;  // skip multiple iterations
  else
    r(k)=i; 
    i=i+1;    // loop increment
    k=k+1;    // vector increment
  end
end
r(k+1:end) = []; // Remove unused portion of the array
Kendra Lynne
  • 397
  • 3
  • 11
  • Thank you for the information including the additional on pre-allocation! – enter_display_name_here Jun 25 '13 at 19:22
  • No problem, I typically work with large vectors, but it's not a bad habit to develop! :) – Kendra Lynne Jun 25 '13 at 19:29
  • 3
    BTW, the reason your code doesn't work is that MATLAB actually stores its own "reference" to the i used in the for loop, so while you can get i, you can't edit the value. I forget exactly how this works, but I remember when I was brand new to programming, I looked this up, kinda interesting haha – Shaun314 Jun 25 '13 at 19:42
  • 1
    @Shaun314 - the thing is that Matlab updates `i` at each loop pass from the array of values on the right hand side of the `for i = [...]` statement, instead of updating it from the previous value of `i` like typical C and Java `for` loops do with something like `for (...; ...; i++)`. So you actually can edit the value of `i`, but your edit will only last until the next loop pass, where Matlab will ignore and overwrite it. (And if it's the last loop pass, then your change will persist.) – Andrew Janke Jul 01 '13 at 04:23
  • This is a much better explanation for what I was trying to get at, thanks! – Shaun314 Jul 01 '13 at 12:56
2

The most basic implementation is to just omit those from the loop.

for i=  [1:3 8:10]
   if i==3
       r(i)=20;
   else
       r(i) = i;
   end
end

However, that may not meet your needs, if you really need to do dynamic determination of loop indexes. In that case, use a while loop, like this:

i = 1;
while i <= 10
   if i==3
       r(i)=20;
       i = i+5;
   else
       r(i) = i;
       i = i+1
   end

end

As you have seen, there are problems when you try and change the indexing variable wihtin a for loop.

Pursuit
  • 12,285
  • 1
  • 25
  • 41
0

If you know where to skip you could do something like

ind = [1:2,8:10]
r(ind) = ind
r(3) = 20

This way you also avoid for loop. If you can't determine how many loops you do before skipping use two different loops and use break keyword to stop the first iteration.

kon psych
  • 626
  • 1
  • 11
  • 26