【发布时间】:2011-11-01 05:54:19
【问题描述】:
下面是我正在处理的一段代码的简化版本(为了避免混淆,省略了很多额外的计算)。它只是cumsum 函数的修改形式。我不想重新发明轮子,那么这个功能已经存在了吗?如果不是,什么方案可以提供最好的速度?
#Set up the data
set.seed(1)
junk <- rnorm(1000000)
junk1 <- rnorm(1000000)
cumval <- numeric(1000000)
#Initialize the accumulator
cumval[1] <- 1
#Perform the modified cumsum
system.time({
for (i in 2:1000000) cumval[i] <- junk[i] + (junk1[i] * cumval[i-1])
})
#Plot the result
plot(cumval, type="l")
【问题讨论】:
-
您介意解释一下它是如何使用的吗?请注意,
junk[1]和junk1[1]永远不会在您的算法中使用...
标签: performance r