title: Select same elements matrix in R
body: <p>I would like select 5 elements, no 25, as fast as possible. It takes a long time run on large vectors:</p>
  
  <pre><code>a = c(1,2,5,2,3)
  b = c(2,4,1,4,5)
  d = matrix(1:25,nrow=5,ncol=5)
  
  result = array(NA,dim=length(a))
  
  for (i in 1:length(a)) { result[i] = d[a[i],b[i]] }
  </code></pre>
  
  <p>OR (more slow)</p>
  
  <pre><code>result&lt;-sapply(1:length(a), function(x)  d[a[x],b[x]] )
  </code></pre>