0

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.

Richie Cotton
  • 118,240
  • 47
  • 247
  • 360
Irene
  • 744
  • 1
  • 12
  • 36
  • 1
    Related question: http://stackoverflow.com/questions/16667247/closest-pair-for-any-of-a-huge-number-of-points/16670220#16670220 – Vincent Zoonekynd Sep 24 '13 at 10:57
  • Can you at least provide your data dimensions? – flodel Sep 24 '13 at 11:39
  • of course sorry, I didn't think it was relevant. Why is it, by the way? At the moment the grid is 400*400 and I need to estimate approximately 100 values of an univariate field. – Irene Sep 24 '13 at 13:10
  • 1
    @Irene the data dims, especially if hugnormous, may require some special packages which handle large data w/o killing RAM. One minor suggestion: you should be able to vectorize your `if` statement and the two-cycle `i` loop. – Carl Witthoft Sep 25 '13 at 12:28

1 Answers1

0

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

 ??apply

You may also want to look in to parallel processing

jsta
  • 3,216
  • 25
  • 35
  • This should probably have been posted as a comment rather than an answer. Also, `*apply` functions are little more than wrappers for `for` - see [here](http://stackoverflow.com/a/2276001/489704) for some discussion. – jbaums Jan 07 '15 at 03:08