title: How to step over a linearized 2D-array with a moving window
body: <p>Assuming I have a 4x4 array that's flatted out in row-major order and I want to collect one entry per 2x2 window in a new 1D array, that would result in:</p>
  
  <p>[0, 2, 8, 10]</p>
  
  <p>for 4x8 it would result in
  [0, 2, 4, 6, 16, 18, 20, 22]</p>
  
  <p>and for 8x4 it would result in
  [0, 2, 8, 10, 16, 18, 24, 26]</p>
  
  <p>simple loop for achieving this:</p>
  
  <pre><code>const int STEP = 2;
  for(int i = 0; i &lt; len; ++i)
    arr[i] =  i * STEP + (i*STEP / SIZE_Y) * SIZE_Y;
  </code></pre>
  
  <p>I run into problems when the array size is odd on either dimension though.
  3x3 should result in</p>
  
  <p>[0, 2, 6, 8] but does in [0, 2, 7, 12]</p>
  
  <p>How do I have to extend my formula so it doesn't get overly complicated treating all these boundary-cases but still works for the general case?</p>