2013-04-18 48 views
7

我正在尝试以少数列和多行排列矩阵中的每一行。 R中是否有矢量化的版本?更具体地说,让我们的种子设置为10,做出了榜样矩阵:在R中向量化顺序

set.seed(10) 
example.matrix = replicate(12,runif(500000)) 

要订购example.matrix,我想,

ordered.example = apply(example.matrix,1,order) 

但是这是非常慢,我喜欢的东西更快。 作为类比,

rowSums(example.matrix) 

优选,

apply(example.matrix,1,sum) 

十分赞赏。

+0

需要8秒,我不会说它很慢:) –

+0

没错。这是一个比我所拥有的尺寸更小的玩具例子,我需要多次这样做。 –

+0

我明白,但重点仍然存在。还有其他几个可以优化速度的选项,例如用C++编写代码,使用并行计算。它们可能会产生更好的效果。 –

回答

3

这里是加速它10倍的方式。它是专门针对您的示例量身定制的,具体取决于您的真实数据是什么样的,这种方法可能或可能不起作用。

我们的想法是,以第一行,1加0至第二等等,然后将其折叠至载体中,那种然后重新组合成一个矩阵:

N = 12; M = 500000; d = replicate(N,runif(M)) 

system.time(d1<-t(apply(d, 1, order))) 
# user system elapsed 
# 11.26 0.06 11.34 

system.time(d2<-matrix(order(as.vector(t(matrix(as.vector(d) + 0:(M-1), nrow = M)))) - 
         rep(0:(M-1), each = N)*N, nrow = M, byrow = T)) 
# user system elapsed 
# 1.39 0.14 1.53 

# Note: for some reason identical() fails, but the two are in fact the same 
sum(abs(d1-d2)) 
# 0 
3

这是一个快一点(键位为order(row(em), em)):

set.seed(10) 
em <- replicate(12,runif(500000)) 
system.time(a <- matrix(em[order(row(em), em)], nrow=nrow(em), byrow=TRUE)) 
# user system elapsed 
# 5.36 0.12 5.80 

set.seed(10) 
example.matrix <- replicate(12,runif(500000)) 
system.time(ordered.example <- apply(example.matrix,1,order)) 
# user system elapsed 
# 13.36 0.09 15.52 

identical(a, ordered.example) 
# [1] FALSE