title: C++: Reshape vector to 3D array
body: <p>Edit: I have uploaded the vector to Drive as a text file, in case anyone want to have a look: <a href="https://drive.google.com/file/d/0B0wsPU8YebRQbDUwNFYza3ljSnc/view?usp=sharing" rel="nofollow noreferrer">https://drive.google.com/file/d/0B0wsPU8YebRQbDUwNFYza3ljSnc/view?usp=sharing</a></p>
  
  <p>I'm trying to reshape my vector <code>h</code> into a 3D array. <code>h</code> contains 295788 elements. In this case <code>height = 314</code>, <code>width = 314</code> and <code>depth = 3</code>. 
  Basically what I'm trying to do is what MATLAB does with its reshape function.</p>
  
  <pre><code>h = reshape(h, height, width, depth)
  </code></pre>
  
  <p>This is my attempt so far, but when I print it all I see is zeroes, which is not right. I have double checked that <code>h</code> contains the numbers I'm expecting. </p>
  
  <pre><code>vector&lt;vector&lt;vector&lt;double&gt; &gt; &gt; array3D;
  
  int height = 314, width = 314, depth = 3;
  // Set up sizes
  array3D.resize(height);
  for (int i = 0; i &lt; height; ++i) {
      array3D[i].resize(width);
  
      for (int j = 0; j &lt; width; ++j)
          array3D[i][j].resize(depth);
  }
  
  for (int i = 0; i &lt; height; i++)
  {
      array3D[i][0][0] = h[i];
      for (int j = 0; j &lt; width; j++)
      {
          array3D[i][j][0] = h[i+j];
          for (int k = 0; k &lt; depth; k++)
          {
              array3D[i][j][k] = h[i+j+k];
          }
      }
  }
  </code></pre>
  
  <p>Printing:</p>
  
  <pre><code>for (vector&lt;vector&lt;vector&lt;double&gt;&gt;&gt;::const_iterator i = array3D.begin(); i != array3D.end(); ++i)
  {
      for (vector&lt;vector&lt;double&gt;&gt;::const_iterator j = i-&gt;begin(); j != i-&gt;end(); ++j)
      {
          for (vector&lt;double&gt;::const_iterator k = j-&gt;begin(); k != j-&gt;end(); ++k)
          {
              cout &lt;&lt; *k &lt;&lt; ' ';
          }
      }
  }
  </code></pre>
  
  <p>So my question is, how do I convert my vector into a 3D array properly?</p>