-2

我有 3 个向量v和。我想找出指标的总和有没有比下面的代码更快的方法?wa(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 
4

1 回答 1

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
于 2013-06-18T06:47:16.730 回答