载体快速矩阵索引 [英] Fast matrix indexing from vectors

查看:153
本文介绍了载体快速矩阵索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做了很多高二维数组的索引矩阵,但指数分裂。我想出了一个解决方案的几个:

I want to do a lot of matrix indexing of a high-D array, but the indices are split up. I came up with a few solutions:

### 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任何东西,我可以在'['?

But the fastest, rbind(c(X)), takes twice as long as indexing directly. Is there any faster way? Is there anything like python's *args that I could run on '['?

推荐答案

一个有点麻烦,但尝试

test[idx1[1], idx1[2], idx2[1], idx2[2]]

这篇关于载体快速矩阵索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆