我正在尝试获取累积总和的向量,也就是说,我有:
# 500 Samples from the U(0,1) Distribution
U<-runif(500,0,1)
# Empty Vector of length 500
F<-rep(0,500)
# Fill the vector with f(U(k))
for ( i in 1:500 ){
F[i] <- sqrt(1-U[i]^2)
}
# Another Empty Vector of length 500
I<-rep(0,500)
# Fill the second empty vector with the sums of F
for ( i in 1:500 ){
I[i]<-cumsum(F[1]:F[i])
}
Run Code Online (Sandbox Code Playgroud)
代码的最后一行是问题,我希望“ I”成为一个向量,使得I [1] = F [1],I [n] = F [1] + F [2] + ..... + F [n]。出于某些原因,cumsum函数无法正常工作。尝试这样做有什么问题?
如果我有误会,请纠正我,但我相信你只是想要这样:
I <- cumsum(sqrt(1 - U^2))
Run Code Online (Sandbox Code Playgroud)
目前尚不清楚为什么要使用for循环。
| 归档时间: |
|
| 查看次数: |
1480 次 |
| 最近记录: |