[R] Vectorizing closest match

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Thu Mar 28 14:50:46 CET 2002

Frank E Harrell Jr <fharrell at virginia.edu> writes:

> If anyone has a very fast vectorized method for doing the following I would appreciate some help.  I want to avoid outer() to limit memory problems for very large n.
> Let
> x = real vector of length n
> y = real vector of length n
> w = real vector of length m, m typically less than n/2 but can be > n
> z = real vector of length m
> For w[i], i=1,,,m, find the value of x that is closest to w[i].  In the case of ties, select one (optimally at random or just take the first match).  Let z[i] = value of y corresponding to the closest x.
> If there is a Fortran or C function (builtin or otherwise) that does much of the work, all the better.

Sounds rather close to what approx() does in the stepfunction cases,
except that you want jumps at the midpoints between the x values
rather than at the x themselves.

   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list