title: Reshape array of images into a column vector, sorted row by row for each image
body: <p>Hello dear Matlab experts,</p>
  
  <p>I have an array of images, let's say a 2 by 3 grid of images (6 images). Each image has a resolution of 4 x 4 (for simplicity). Lets say the images are grayscale. </p>
  
  <p>I loaded the images into a 4D matrix with dimensions 2 x 3 x 4 x 4. Now I want to create a column vector with entries</p>
  
  <p>1: first pixel of first row in image 1,1</p>
  
  <p>2: second pixel of first row in image 1,1</p>
  
  <p>3: ...</p>
  
  <p>16: last pixel of last row in image 1, 1</p>
  
  <p>17: first pixel of first row in image 2, 1</p>
  
  <p>...</p>
  
  <p>and so on goes the pattern. I could successfully create this with a bunch of for loops:</p>
  
  <pre><code>for imageX = 1 : resolution(2)
     for imageY = 1 : resolution(1)
        for pixelX = 1 : resolution(4)
           for pixelY = 1 : resolution(3)
  
                 % linear index for 4D indices
                 row = ((imageY - 1) * resolution(2) + imageX - 1) * resolution(3) * resolution(4) + ...
                     (pixelY - 1) * resolution(4) + pixelX;
  
                 lightFieldVector(row) = lightField(imageY, imageX, pixelY, pixelX);
           end
        end
     end
  end
  </code></pre>
  
  <p>I am wondering if this ugly pile of loops can be replaced by a few <code>reshape</code> and <code>permute</code> operations. I think so, but I have trouble finding the right order. I used these methods a few times, but only with 2D matrices. From the documentation I can conclude that reshaping will go 'column first', so this would be the opposite I need. </p>
  
  <p>I appreciate your help,
  Adrian</p>