0
在我的隨機漫步循環中,我需要合併一個使用步驟編號作爲變量的函數。如何在隨機遊走中加入步數計數器?
在隨機行走過程中,我如何讓R產生一個步數(循環計數可能是一個更好的術語)?
我需要的是正確的,在這個代碼,我目前擁有的虛擬代碼step.num
僅供參考我的隨機遊走的代碼的末尾:
walkW <- function(n.times=125,
xlim=c(524058,542800),
ylim=c(2799758,2818500),
start=c(542800,2815550),
stepsize=c(4000,4000)) {
plot(c(0,0),type="n",xlim=xlim,ylim=ylim,
xlab="Easting",ylab="Northing",col="white",col.lab="white")#arena
x <- start[1]
y <- start[2]
steps <- 1/c(1,2,4,8,12,16)
steps.y <- c(steps,-steps,0)
steps.x <- c(steps[c(1,5,6)],-steps,0)
points(x,y,pch=16,col="green",cex=1)
for (i in 1:n.times) {
repeat {
xi <- stepsize[1]*sample(steps.x,1)
yi <- stepsize[2]*sample(steps.y,1)
newx <- x+xi
newy <- y+yi
if (newx>xlim[1] && newx<xlim[2] &&
newy>ylim[1] && newy<ylim[2]) break
}
lines(c(x,newx),c(y,newy),col="white")
x <- newx
y <- newy
}
step.num<-(your magical step counter function) #I need the step counter here
}
set.seed(101)
walkW(s)
你也可能喜歡'all'功能。你的休息條件可以被重寫,如果(所有(newx> xlim [1],newx ylim [1],newy
Gregor
2014-10-29 22:32:05
非常棒的你們。我實際上只是彈出來提供相同的答案。當魔法如x = y那樣簡單時,它總是很棒。謝謝一堆! – Jesse001 2014-10-29 22:33:19