开发者

R vector loose one component when passed

recursiveCall <- function(x, N)
{
    cat("length = ", length(x))
    cat("vector x = ", x[1:2^N], "\n")
    return (x)
}
PaulLevyBrownianMotion <- function(N)
{
    cat("Paul Levy construction for N = ", N, "\n")
    W = c(rnorm(2^N+1, 0, 1))
    cat("length = ", length(W))
    cat("Wstandard = ", W, "\n")
    W <- recursiveCall(W[1:2^N+1], N)
    return (W) 
}

My vector W seems to lost its first component when passed to another function. Could you help me with this ? Here is the output.

> W = PaulLevyBrownianMotion(2)
Paul Levy construction for N =  2 
length =  5Wstandard =  0.08641454 1.616638 -0.8747996 0.6149899开发者_如何学Python 0.2689501 
length =  4vector x =  1.616638 -0.8747996 0.6149899 0.2689501 
> 


W[1:2^N + 1] isn't indexing what you think because of precedence. First the vector 1:2^N is constructed and then scalar 1 is added (so each element is incremented by one), resulting in elements 2 through the end being selected.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜