Frage

I need to calculate the distance between a grid that is the output of my kriging interpolation and some points where i need to interpolate.

Problem is that the grid is quite big and making the banal double for cycle calculating the distance between the points of the grid and my points of interest with geodDist from the oce package takes forever.

Is there a better way to do calculate which point in a grid is closer to some points of interest??

This is my banal cycle

#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
    } 
  }
}

Since it is just a best practice question I don't include any data, hope it is ok.

War es hilfreich?

Lösung

As Carl suggests, using the apply family of functions may speed up calculations

 ??apply

You may also want to look in to parallel processing

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top