title: How to apply function over each matrix element's indices
body: <p>I am wondering if there is a built-in function in R which applies a function to each element of the matrix (of course, the function should be computed based on matrix indices). The equivalent would be something like this:</p>
  
  <pre><code>matrix_apply &lt;- function(m, f) {
    m2 &lt;- m
    for (r in seq(nrow(m2)))
      for (c in seq(ncol(m2)))
        m2[[r, c]] &lt;- f(r, c)
    return(m2)
  }
  </code></pre>
  
  <p>If there is no such built-in function, what is the best way to initialize a matrix to contain values obtained by computing an arbitrary function which has matrix indices as parameters?</p>