Question: I don't understand this piece of code of a `for` loop in matlab, I know that loops in matlab usually look like: `for ii=1:2:100` so that it starts in 1 until 100 and in each iteration you add 2.
But here I've got this condition in the loop and I don't get what it does: 

```
for ii=[1:w:rd(1)-w-border, rd(1)-w-border+1],
   ...
end;

```

`w` and `border` are integers passed as arguments and `rd` is size of the image/matrix (`rd = size(image);`)

Can someone explain me how for loops work in matlab with this kind of condition? 
Thanks in advance.

Answer: 
You're not doing it wrong, that's just not the semantics defined for [`Enumerator#each`](http://www.ruby-doc.org/core-2.0/Enumerator.html#method-i-each). You could make a derivative enumerator that only iterates from current position to end:

```
class Enumerator
  def enum_the_rest
    Enumerator.new { |y| loop { y << self.next } }
  end
end

o = Object.new
def o.each
  yield 1
  yield 2
  yield 3
end

e = o.to_enum
=> #<Enumerator: ...>
e.next
=> 1
e2 = e.enum_the_rest
=> #<Enumerator: ...>
e2.each { |x| puts x }
=> 2
=> 3

```

And, BTW, `each` doesn't restart the sequence, it just always runs over the entire span. Your enumerator still knows where it is in relation to the next `next` call.

```
e3 = o.to_enum
e3.next
=> 1
e3.next
=> 2
e3.map(&:to_s)
=> ["1", "2", "3"]
e3.next
=> 3

```
[`Enumerator#next`](http://ruby-doc.org/core-2.0/Enumerator.html#method-i-next) and [`Enumerator#each`](http://ruby-doc.org/core-2.0/Enumerator.html#method-i-each) work on the object differently. Per the documentation for `#each` (emphasis mine):

> 
> Iterates over the block **according to how this Enumerable was constructed**. If no block is given, returns self.
> 
> 
> 

So `#each` always behaves based on the original setup, not on the current internal state. If you quickly peak at the source you'll see that `rb_obj_dup` is called to setup a new enumerator.