2014-10-27 77 views
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) 

在此先感謝您的幫助!

回答

1

哇,我現在覺得啞巴。答案很簡單!我的舞臺是由陳述

   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") 

代表引用的圖像在實際的GPS座標的約束,但step.prob函數引用像素座標,而不是舞臺coordinates.The問題從簡單的事實,我的作用是來了引用錯誤的比例。

要解決它,我看了看圖像的尺寸我使用的是

dim(pic) 

它告訴我的圖像648 * 615 然後我重新寫了舞臺上是一個完美的正方形在這個尺度上代替:

  xlim=c(0,615), 
      ylim=c(0,615), 
      start=c(307,307) 

問題修正!