title: Reshape 2-D matrix with a specific format into a 3-D matrix (MATLAB)
body: <p>I have a Cartesian 2-D matrix, <code>A</code>, that contains bins of dose values in three (X,Y,Z) dimensions;</p>
  
  <ul>
  <li>X coordinate: from 0.0 to 50.00 cm, <code>nx=512</code> bins (9.7656E-02 cm wide)</li>
  <li>Y coordinate: from 0.0 to 50.00 cm, <code>ny=512</code> bins (9.7656E-02 cm wide)</li>
  <li>Z coordinate: from 0.0 to 27.12 cm, <code>nz=113</code> bins (2.4000E-01 cm wide)</li>
  </ul>
  
  <p>Therefore, the total number of elements in <code>A</code> is <code>nx*ny*nz=29622272</code>. When reading a .dat-file, the data in matrix <code>A</code> are stored in the following format</p>
  
  <blockquote>
    <p>A(1,1,1) A(2,1,1) A(3,1,1) A(4,1,1) A(5,1,1) A(6,1,1) A(7,1,1) A(8,1,1) A(9,1,1) A(10,1,1)</p>
    
    <p>A(11,1,1) A(12,1,1) A(13,1,1) A(14,1,1) A(15,1,1) A(16,1,1) A(17,1,1) A(18,1,1) A(19,1,1) A(20,1,1)</p>
    
    <p>…</p>
    
    <p>A(503,1,1) A(504,1,1) A(505,1,1) A(506,1,1) A(507,1,1) A(508,1,1) A(509,1,1) A(510,1,1) A(511,1,1) A(512,1,1)</p>
    
    <p>A(1,2,1) A(2,2,1) A(3,2,1) A(4,2,1) A(5,2,1) A(6,2,1) A(7,2,1) A(8,2,1) A(9,2,1) A(10,2,1)</p>
    
    <p>A(11,2,1) A(12,2,1) A(13,2,1) A(14,2,1) A(15,2,1) A(16,2,1) A(17,2,1) A(18,2,1) A(19,2,1) A(20,2,1)</p>
    
    <p>…</p>
    
    <p>…</p>
    
    <p>A(1,1,2) A(2,1,2) A(3,1,2) A(4,1,2) A(5,1,2) A(6,1,2) A(7,1,2) A(8,1,2) A(9,1,2) A(10,1,2)</p>
    
    <p>…</p>
    
    <p>A(511,512,113) A(512,512,113) </p>
  </blockquote>
  
  <p>where the first index <code>ix</code> is assigned to the X coordinate, index <code>iy</code> is assigned to the Y coordinate and index <code>iz</code> is assigned to the Z coordinate in <code>A(ix,iy,iz)</code>, and the first index runs faster than the second, and the second faster than the third. So when reading the matrix from the input file, <code>A</code> has the size <code>[2962228 10]</code> (the last row in the input file contains only two elements/columns). </p>
  
  <p>I want to reshape this matrix into an equivalent Cartesian 3-D matrix, <code>B(ix,iy,iz)</code>, of dimensions <code>[nx ny nz] = [512 512 113]</code> - is there an efficient way to do this? Also, 8 extra elements are appended in the last row when reading by <code>fscanf()</code> since <code>A</code> contains 10 elements per row - how do I not include these values when reshaping?</p>
  
  <p>Many thanks in advance!!</p>