title: Subsetting multidimensional array using matrix in R
body: <p>I have a 4 dimensional array of water temperatures [lon, lat, depth, month] and I want to extract monthly temperatures based on a matrix where the rows and columns are lon and lat respectively and the values the depth levels. The result would be a 3 dimensional array of monthly temperatures at variable depth [lon, lat, month].</p>
  
  <p>Just to provide an example:</p>
  
  <pre><code>set.seed(1)    
  temperature &lt;- array(rnorm(10), rep(3,4))
  depth &lt;- matrix(c(NA, sample(1:3, 8, replace = TRUE)), 3, 3)
  </code></pre>
  
  <p>I had a look at <a href="https://stackoverflow.com/questions/11514964/how-to-index-a-multidimensional-r-array-dynamically?rq=1">How to index a multidimensional R array dynamically?</a> and <a href="https://stackoverflow.com/questions/34795331/r-use-matrix-to-select-row-of-multidimensional-array?lq=1">R use matrix to select row of multidimensional array</a> but can't work out how to do this?</p>
  
  <p>Any help is much appreciated.</p>