问题
I'm building a biased correlated random walk, and I've managed to build the RW, and bias it for westerly movement.
The issue: I need the walk to be bound on one (or all) side(s).
The current code is:
walk<-function(n.times){
plot(524058:542800,2799758:2818500,type="n",
xlab="Easting",ylab="Northing")#arena
y<-2815550 ##startY
x<-542800 #startX
N<-4000
E<-4000
points(x,y,pch=16,col="red",cex=1)
for (i in 1:n.times) {
yi <- sample(c(N,N/2,N/4,N/8,N/12,N/16,
0,-N,-N/2,-N/4,-N/8,-N/12,-N/16),1)
xi<-sample(c(E,E/12,E/16, 0,-E,-E/2,-E/4,-E/8,-E/12,-E/16),1)
lines(c(x,x+xi),c(y,y+yi),col="blue")
x<-x+xi
y<-y+yi
}
}
iterations<-125
walk(iterations)
So far the closest I've come is using
if(y>2818500 | y<2799758 | x>542800 | x<524058) break
which simply stops the walk if it leaves the arena.
来源:https://stackoverflow.com/questions/26390127/how-do-i-put-arena-limits-on-a-random-walk