tags:

views:

53

answers:

2

I have X, a three-dimensional array in R. I want to take a vector of indices indx (length equal to dim(X)[1]) and form a matrix where the first row is the first row of X[ , , indx[1]], the second row is the second row of X[ , , indx[2]], and so on.

For example, I have:

R> X <- array(1:18, dim = c(3, 2, 3))
R> X
, , 1

     [,1] [,2]
[1,]    1    4
[2,]    2    5
[3,]    3    6

, , 2

     [,1] [,2]
[1,]    7   10
[2,]    8   11
[3,]    9   12

, , 3

     [,1] [,2]
[1,]   13   16
[2,]   14   17
[3,]   15   18

R> indx <- c(2, 3, 1)

My desired output is

R> rbind(X[1, , 2], X[2, , 3], X[3, , 1])
     [,1] [,2]
[1,]    7   10
[2,]   14   17
[3,]    3    6

As of now I'm using the inelegant (and slow) sapply(1:dim(X)[2], function(x) X[cbind(1:3, x, indx)]). Is there any way to do this using the built-in indexing functions? I had no luck experimenting with the matrix indexing methods described in ?Extract, but I may just be doing it wrong.

+1  A: 

Maybe like this:

t(sapply(1:3,function(x) X[,,idx][x,,x]))
mbq
+1  A: 

I may be answering the wrong question (I can't reconcile your first description and your sample output)... This produces your sample output, but I can't say that it's much faster without running it on your data.

do.call(rbind, lapply(1:dim(X)[1], function(i) X[i, , indx[i]]))

richardh