R 向量在传递时会丢失一个组件

发布于 2024-12-10 15:07:40 字数 695 浏览 1 评论 0原文

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) 
}

我的向量 W 在传递给另一个函数时似乎丢失了其第一个分量。你能帮我解决这个问题吗?这是输出。

> W = PaulLevyBrownianMotion(2)
Paul Levy construction for N =  2 
length =  5Wstandard =  0.08641454 1.616638 -0.8747996 0.6149899 0.2689501 
length =  4vector x =  1.616638 -0.8747996 0.6149899 0.2689501 
> 
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 0.2689501 
length =  4vector x =  1.616638 -0.8747996 0.6149899 0.2689501 
> 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

还在原地等你 2024-12-17 15:07:40

由于优先级,W[1:2^N + 1] 并未对您的想法进行索引。首先构造向量 1:2^N,然后添加标量 1(因此每个元素递增 1),从而选择从 2 到末尾的元素。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文