title: How to reshape matrices in Mathematica
body: <p>When manipulating matrices it is often convenient to change their shape. For instance, to turn an N x M sized matrix into a vector of length N X M. In MATLAB a <em>reshape</em> function exists:</p>
  
  <blockquote>
    <p>RESHAPE(X,M,N) returns the M-by-N matrix whose elements are taken columnwise from X.  An error results if X does not have M*N elements.</p>
  </blockquote>
  
  <p>In the case of converting between a matrix and vector I can use the Mathematica function <em>Flatten</em> which takes advantage of Mathematica's nested list representation for matrices. As a quick example, suppose I have a matrix X:</p>
  
  <p><img src="https://imgur.com/TIYq9.jpg" alt="4x4 matrix"></p>
  
  <p>With <em>Flatten</em>[X] I can get the vector {1,2,3,...,16}. But what would be far more useful is something akin to applying Matlab's <em>reshape</em>(X,2,8) which would result in the following Matrix:</p>
  
  <p><img src="https://imgur.com/Xe6aB.jpg" alt="4x4 matrix"></p>
  
  <p>This would allow creation of arbitrary matrices as long as the dimensions equal N*M. As far as I can tell, there isn't anything built in which makes me wonder if someone hasn't coded up a Reshape function of their own.</p>