gpt4 book ai didi

r - 如何相对于最后一个值镜像/反射向量

转载 作者:行者123 更新时间:2023-12-04 01:01:46 26 4
gpt4 key购买 nike

vector相对于最后一个值如何体现,我不知道怎么用文字来解释,所以我用图片来展示 enter image description here

enter image description here

我试着自己做,但我的代码只适用于线性向量

x <- 1:10 # some linear vector
y <- rev(cumsum(c(tail(x,1),diff(x))))

plot(rep(NA,20),t="l" , ylim = c(1,20))
lines(x,col=2,lwd=5)
lines(y,col=4,lty=5)

enter image description here

如果我采用任意向量,我的代码将不起作用

set.seed(123)
x <- cumsum(rnorm(10)) # real vector
y <- rev(cumsum(c(tail(x,1),diff(x))))

plot(rep(NA,20),t="l" , ylim = c(-5,10))
lines(x,col=2,lwd=5)
lines(y,col=4,lty=5)

enter image description here

最佳答案

我觉得你追求的是

set.seed(123)
x <- cumsum(rnorm(10))

y <- x-2*(x-tail(x,1))

plot(rep(NA,20),t="l" , ylim = c(-5,10))
lines(x,col=2,lwd=5)
lines(y,col=4,lty=5)

x-2*(x-tail(x,1)) 部分相对于行中的最后一个点调整每个点。基本上你找到距离并减去两倍的距离以移动最后一点的“相反侧”。您可以移动术语以简化为

y <- 2*tail(x,1)-x

enter image description here

关于r - 如何相对于最后一个值镜像/反射向量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68107467/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com