title: Loop through array in R
body: <p>I got the following issue:</p>
  
  <p>I have a large array "x" of the dimension dim(x)= (46, 13, 30).
  I'm trying to compute a new matrix "M"(30,598) which contains basically the first element of every "slice" in the first column, the second element in the second and so on... Eventually, I want to work with vectors (columns of M) containing all the elements of each grid cell.</p>
  
  <p>With the following function I would be able to calculate my matrix M. However, it's not exactly a user-friendly solution considering the large array:</p>
  
  <pre><code>M &lt;- cbind(x[1,1,],x[1,2,],x[1,3,],x[1,4,],x[2,1,],x[2,2,], x[2,3,],x[2,4,]......)
  </code></pre>
  
  <p>That's why I want to use a for-loop. Unfortunately I can't figure out how.</p>
  
  <p>This is what I tried so far (unsuccessfully):</p>
  
  <pre><code>M  &lt;- matrix(NA, nrow = 6, ncol = 20)
  for (i in 1:120){
      M[i] &lt;- cbind(x[,,i])
  } 
  </code></pre>
  
  <p>This loop only creates the first vector (first element of every "slice" [1,1,]), than aborts.</p>
  
  <p>Any ideas on how to get this done? 
  Very much appreciate the help!</p>