gpt4 book ai didi

r - 如何评估和修复 “subscript out of bounds”错误?

转载 作者:行者123 更新时间:2023-12-03 08:13:31 24 4
gpt4 key购买 nike

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

问题已解决!

关于r - 如何评估和修复 “subscript out of bounds”错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26592355/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com