【问题标题】:How do I evaluate and fix "subscript out of bounds" error?如何评估和修复“下标越界”错误?
【发布时间】:2014-10-27 16:36:07
【问题描述】:

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

提前感谢您的帮助!

【问题讨论】:

    标签: r error-handling


    【解决方案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 函数引用的是像素坐标而不是竞技场坐标。问题来自一个简单的事实,即我的函数引用了错误的比例。

    为了解决这个问题,我查看了我正在使用的图像的尺寸

    dim(pic)
    

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

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

    问题已解决!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-30
      • 2018-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多