title: Index value for matrix in R?
body: <p>Is there a function to get an index (row number and column number) for a matrix?  </p>
  
  <p>Suppose that I have a simple matrix:</p>
  
  <pre><code>a &lt;- matrix(1:50, nrow=5)
  </code></pre>
  
  <p>Is there an easy way to get back something like c(3, 5) for the number "23", for instance?  In this case, saying <code>which(a==23)</code> just returns 23.  </p>
  
  <p>This seems to work but I'm sure that there's a better way:</p>
  
  <pre><code>matrix.index &lt;- function(a, value) {
    idx &lt;- which(data.frame(a)==value)
    col.num &lt;- ceiling(idx/nrow(a))
    row.num &lt;- idx - (col.num-1) * nrow(a)
    return(c(row.num, col.num))
  }
  &gt; matrix.index(a, 23)
  [1] 3 5
  &gt; matrix.index(a, 50)
  [1]  5 10
  </code></pre>