0

我想做很多高维数组的矩阵索引,但是索引是分开的。我想出了几个解决方案:

### setup
test <- array(0, c(3,3,3,3))
test[1,2,3,2] <- 1
system.time(for (i in 1:1000000) test[1,2,3,2] )
### index split between two vectors
idx1 <- c(1,2);     idx2 <- c(3,2)
### things that work are slower
system.time(for (i in 1:1000000) test[rbind(c(idx1, idx2))] )
system.time(for (i in 1:1000000) test[matrix(c(idx1, idx2), nrow=1)] )
system.time(for (i in 1:1000000) test[t(c(idx1, idx2))] )

但是最快的 rbind(c(X)) 花费的时间是直接索引的两倍。有没有更快的方法?有没有像 python 的 *args 我可以在 '[' 上运行的东西?

4

1 回答 1

1

有点麻烦,但是试试

test[idx1[1], idx1[2], idx2[1], idx2[2]]
于 2013-11-06T11:38:42.523 回答