0
當我運行下面的代碼時,我收到:「eval(expr,envir,enclos)中的錯誤:下標越界」如何評估和修復「下標越界」錯誤?
通過排除過程,我將問題縮小到step.prob函數,但我似乎無法進一步調試它。
我已閱讀關於此錯誤的其他問題,但尚未找到有用的答案/我不知道如何更改答案以適合我的情況。
主要問題:如何調試下標越界錯誤?
P<-30
step.max=125
s<-step.max
walkW <- function(n.times=125,
xlim=c(524058,542800),
ylim=c(2799758,2818500),
start=c(542800,2815550),
stepsize=c(4000,4000)) {
pic<-readImage("yourpic.png",all=TRUE,package="EBImage") #use whatever binary image you have on hand
plot(c(0,0),type="n",xlim=xlim,ylim=ylim,
xlab="Easting",ylab="Northing")
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
##The error is coming from the following function
step.prob<-function(n.times=step.max){
CS<-pic[x,y,1]
CS.max<-1
step.num<-15
SP<-(((CS/CS.max)*(1-(step.num/step.max))+(step.num/step.max))*100)
}
z<-step.prob(1)
##end of broken function
if(z>P)break
else
if(step.max){points(newx,newy,pch=16,col="yellow",cex=1)
}
}
}
set.seed(101)
walkW(s)
在此先感謝您的幫助!