如何评估和修复“下标越界”错误?

时间:2021-11-09 16:40:47

When I run the code below I receive: "Error in eval(expr, envir, enclos) : subscript out of bounds"

当我运行下面的代码时,我收到:“eval中的错误(expr,envir,enclos):下标超出范围”

Through a process of elimination I've narrowed the issue down to the step.prob function, but I can't seem to debug it further than that.

通过消除过程,我将问题缩小到step.prob函数,但我似乎无法进一步调试它。

I've read the other questions regarding this error but haven't found the answers useful/I don't know how to alter the responses to fit my situation.

我已经阅读了有关此错误的其他问题,但没有找到有用的答案/我不知道如何更改响应以适应我的情况。

Main question: How do I debug the subscript out of bounds error?

主要问题:如何调试下标越界错误?

        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)

Thanks in advance for all your help!

在此先感谢您的帮助!

1 个解决方案

#1


1  

Wow I feel dumb now. The answer is simple! My arena is bound by statement

哇我现在感到愚蠢。答案很简单!我的竞技场受到声明的约束

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

which represents the actual GPS coordinates in the referenced image, but the step.prob function was referencing pixel coordinates rather than arena coordinates.The issue came from the simple fact that my function was referencing the wrong scale.

它表示参考图像中的实际GPS坐标,但step.prob函数是引用像素坐标而不是竞技场坐标。问题来自于我的函数引用了错误的比例的简单事实。

To fix it I looked at the dimensions of the image I'm using with

为了解决它,我查看了我正在使用的图像的尺寸

dim(pic)

which told me the image was 648 * 615. Then I re-wrote the arena to be a perfect square on that scale instead:

这告诉我图像是648 * 615.然后我重新写了竞技场,成为一个完美的广场,而不是:

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

Problem fixed!

问题解决了!

#1


1  

Wow I feel dumb now. The answer is simple! My arena is bound by statement

哇我现在感到愚蠢。答案很简单!我的竞技场受到声明的约束

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

which represents the actual GPS coordinates in the referenced image, but the step.prob function was referencing pixel coordinates rather than arena coordinates.The issue came from the simple fact that my function was referencing the wrong scale.

它表示参考图像中的实际GPS坐标,但step.prob函数是引用像素坐标而不是竞技场坐标。问题来自于我的函数引用了错误的比例的简单事实。

To fix it I looked at the dimensions of the image I'm using with

为了解决它,我查看了我正在使用的图像的尺寸

dim(pic)

which told me the image was 648 * 615. Then I re-wrote the arena to be a perfect square on that scale instead:

这告诉我图像是648 * 615.然后我重新写了竞技场,成为一个完美的广场,而不是:

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

Problem fixed!

问题解决了!