R 向量在传递时会丢失一个组件
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于优先级,
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 vector1:2^N
is constructed and then scalar1
is added (so each element is incremented by one), resulting in elements 2 through the end being selected.