arrays - Select same elements matrix in R -


I probably have to select 5 elements as soon as possible, no. It takes a long time on large vectors:

  a = c (1,2,5,2,3) b = c (2,4,1,4,5) d = Results for the matrix (1: 25, nrow = 5, ncol = 5) = array (NA, dim = length (A)) (i in 1: length (a)) {result [i] = d [a [i] ], B [i]]}  

or (more slow)

  results & lt; -sapply (1: length (A), function (x) d   
  d [cbind (a, b)] # [1] 6 17 5 17 23  

For more details, remove , where you will find the following lines:

A third form of indexing with a column for each dimension A numeric matrix is ​​done through: Each row of the index matrix then selects a single element of the array, and the result is a vector.

On the same help page, "Example" In the section There are some examples.


Comments

Popular posts from this blog

HTML/CSS - Automatically set height width from background image? -

php - Mysql Show Process - Sleep Commands and what to do -

c - What is the address of buf (the local variable in the main function)? -