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