matrix - Quickest distance computation between two large vectors in R -


i wish calculate distance between each element in 1 vector , each element in vector in quickest possible way in r. small example is:

distf<-function(a,b) abs(a-b) x<-c(1,2,3) y<-c(1,1,1) result<-outer(x,y, distf) 

the issue x , y of length 30,000 each , r crashes while trying computation. , doing once, have repeat process 1000 times in simulation study. there quicker functions able achieve this?

i need identify of these distances less fixed number/calliper. studying many such fixed callipers eventually, therefore, need save these distances, if computation demanding. function called caliper in r package optmatch process directly, cannot handle such big computation well.

here's rcpp version returns integer matrix of 1s , 0s dependent on whether each pair wide comparison <= threshold. on machine took 22.5 secs 30,000 30,000. output matrix little under 7 gb in ram though.

fast_cal.cpp

#include <rcpp.h>  using namespace rcpp;  // [[rcpp::export]] numericmatrix fast_cal(numericvector x, numericvector y, double threshold) {   const long nr=x.length();   const long nc=y.length();   numericmatrix output(nr, nc);   (long i=0; i<nr; i++) {     (long j=0; j<nc; j++) {       output(i, j) = (fabs(x(i) - y(j)) <= threshold) ? 1 : 0;     }   }   return output; } 

testing

library("rcpp") sourcecpp("fast_cal.cpp") x <- rnorm(30000) y <- rnorm(30000) out <- fast_cal(x, y, 0.5) 

Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -