title: replace diagonal elements in an array
body: <p>Does anyone know a neat/efficient way to replace diagonal elements in array, similar to the use of <code>diag(x) &lt;- value</code> for a matrix? In other words something like this:</p>
  
  <pre><code>&gt; m&lt;-array(1:27,c(3,3,3))
  &gt; for(k in 1:3){
  +   diag(m[,,k])&lt;-5
  + }
  &gt; m
  , , 1
  
       [,1] [,2] [,3]
  [1,]    5    4    7
  [2,]    2    5    8
  [3,]    3    6    5
  
  , , 2
  
   [,1] [,2] [,3]
  [1,]    5   13   16
  [2,]   11    5   17
  [3,]   12   15    5
  
  , , 3
  
       [,1] [,2] [,3]
  [1,]    5   22   25
  [2,]   20    5   26
  [3,]   21   24    5
  </code></pre>
  
  <p>but without the use of a for loop (my arrays are pretty large and this manipulation will already be within a loop).</p>
  
  <p>Many thanks.</p>