R中的累积和向量
我试图得到一个累积和的向量,也就是说,我有:R中的累积和向量,r,cumulative-sum,R,Cumulative Sum,我试图得到一个累积和的向量,也就是说,我有: # 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 len
# 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])
}
#来自U(0,1)分布的500个样本
如果我有误解,请纠正我,但我相信你只是想要这个:
I <- cumsum(sqrt(1 - U^2))
我是。。我认为它不会应用于每个元素。我认为for循环将确保每个元素都被转换。。。现在看起来很傻,如果我想让我的每个元素都达到平均值呢?即I[3]=(F[1]+F[2]+F[3])/3,那么我必须使用for循环吗?不,只需执行I/(沿(I)顺序执行)
。