R 与栅格的距离和一组单独的点(无循环)

R 与栅格的距离和一组单独的点(无循环),r,matrix,distance,spatial,gstat,R,Matrix,Distance,Spatial,Gstat,我需要计算作为克里格插值输出的网格和我需要插值的一些点之间的距离 问题是网格相当大,使用geodDist从oce包计算网格点和我感兴趣的点之间的距离要花很长时间 有没有更好的方法来计算网格中哪个点更接近某些感兴趣的点 这是我平淡无奇的循环 #find the closest points from the grid to the old samples #kriging model and so on y_ok now contains the grid y_ok <- krige(rs

我需要计算作为克里格插值输出的网格和我需要插值的一些点之间的距离

问题是网格相当大,使用
geodDist
oce
包计算网格点和我感兴趣的点之间的距离要花很长时间

有没有更好的方法来计算网格中哪个点更接近某些感兴趣的点

这是我平淡无奇的循环

#find the closest points from the grid to the old samples
#kriging model and so on y_ok now contains the grid

y_ok <- krige(rssi~1, samples, predgrid, model = vfit_ok, nmax=5)

yok.fr<-as.data.frame(y_ok)
#samples_all.fr contains the points where I want to interpolate
require(oce)
dist.mtx<-matrix(data=NA,nrow=dim(samples_all.fr)[1],ncol=2)

for (i in 1:2){#dim(samples_all.fr[1])){
  for(j in 1:dim(yok.fr)[1]){
    a=geodDist(samples_all.fr[i,2], samples_all.fr[i,1], yok.fr[j,2], yok.fr[j,1])
    if(!(is.finite(dist.mtx[i,1]))|(a<dist.mtx[i,1])){
      dist.mtx[i,1]=a
      dist.mtx[i,2]=j
    } 
  }
}
#找到从网格到旧样本最近的点
#克里格模型等现在包含网格

y_ok正如Carl所建议的,使用apply函数族可以加快计算速度

 ??apply

你可能还想看看并行处理相关的问题:你能至少提供你的数据维度吗?当然对不起,我认为这不相关。顺便问一下,为什么会这样?目前网格为400*400,我需要估计一个单变量字段的大约100个值。@Irene数据dims,特别是如果非常大,可能需要一些特殊的包来处理不带RAM的大数据。一个小建议:你应该能够向量化你的
if
语句和两个循环
i
循环。这可能是作为注释而不是答案发布的。另外,
*apply
函数只不过是
for
的包装器-有关一些讨论,请参阅。