r语言 - 如何评估和修复"subscript out of bounds"错误?



当我运行下面的代码时,我收到:"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)

提前感谢您的帮助!

哇,我现在觉得很笨。答案很简单!我的竞技场受声明约束

               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函数是参考像素坐标而不是竞技场坐标。这个问题源于一个简单的事实,即我的职能引用了错误的比额表。

为了修复它,我查看了使用的图像的尺寸

dim(pic)

它告诉我图像是648*615。然后我把竞技场重新写成了一个完美的正方形:

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

问题已解决!

相关内容

最新更新