title: how to traverse an n-dimensional array with stride
body: <p>I have an indexing issue that I am trying to solve. 
  I have a n-dimensional array with known shape.
  I would like to traverse the array with a stride (possibly different in each dim).</p>
  
  <p>With fixed dimensions, I would do this with nested for loops (small arrays) and increment by the stride:</p>
  
  <pre><code>std::vector&lt;int&gt; shape = {10, 10}; // h,w
  int num_dim = shape.size();
  std::vector&lt;int&gt; stride = {1,2};
  
  for (int i = 0; i&lt; shape[0]; i+=stride[0]) {
      for (int j = 0; j&lt; shape[1]; j+=stride[1]) {
       //print flattened index (row major)
       printf("index: %d\n",i*shape[0]+j);
      }
  
  }
  </code></pre>
  
  <p>But how would I do this with an n-dimensional array (flattened)?
  I.e. something like:</p>
  
  <pre><code>std::vector&lt;int&gt; shape = {10, 10}; // h,w
  int num_dim = shape.size();
  std::vector&lt;int&gt; stride = {1,2};
  
  int shape_size = 1;
  for (int i = 0; i&lt; num_dim; ++i) {
  shape_size *= shape[i];
  }
  
  int ind = 0;
  while (ind &lt; shape_size) {
   // somehow incr ind by the correct amount according to stride, and shape
   // or check if the ind is in the stride (less desirable)
  }
  </code></pre>