How do I put arena limits on a random walk?

十年热恋 提交于 2019-12-21 14:12:23

问题


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")#aren‌​a 
      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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!