通过R中的向量获得矩阵乘法的rowSum的最快方法

时间:2013-06-18 04:43:56

标签: r

我有3个向量vwa。我想找出指标(v > w_i)* a_i.的总和是否有比跟随代码更快的方法?

v = rnorm(1600)
w = runif(500)
a = rnorm(500)
m = v > rep(w, each = length(v))
dim(m)=c(length(v), length(w))

. system.time({
    m = v > rep(w, each = length(v))
    dim(m)=c(length(v), length(w))
    rowSums(m %*% diag(a))
  })
   user  system elapsed 
  0.03    0.00    0.04 

1 个答案:

答案 0 :(得分:1)

即使是非矢量化解决方案也会比设置diag所做的巨型稀疏矩阵更快。

system.time(
  res<-sapply(v,function(v1)sum(a[v1>w]))
) 
#   user  system elapsed 
#  0.032   0.000   0.031

system.time({
  m = v > rep(w, each = length(v))
  dim(m)=c(length(v), length(w))
  res<-rowSums(m %*% diag(a))
})
#  user  system elapsed 
# 0.364   0.000   0.362 

但是,如果你想获得幻想,你可以这样做:

fancy<-function(){
  order.w<-order(w)
  cumsum.a<-c(0,cumsum(a[order.w]))
  cumsum.a[findInterval(v,c(-Inf,w[order.w]))]
}
system.time(res2<-fancy())
#   user  system elapsed 
#  0       0       0 
all.equal(res,res2)
# TRUE