mercredi 23 juin 2021

How to mirror/reflect a vector relative to the last value

how can I reflect the vector relative to the last value, I don't know how to explain it in words, so I will show it with pictures enter image description here

enter image description here

I tried to do it myself, but my code only works with linear vector

x <- 1:10
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

If you take an arbitrary vector, my code doesn't work

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





Aucun commentaire:

Enregistrer un commentaire