I have two vectors of integers, and for each element of the second vector I want to find the minumum distance to any element of the first vector - for example
obj1 <- seq(0, 1000, length.out=11)
obj2 <- 30:50
min_diff <- sapply(obj2, function(x) min(abs(obj1-x)))
min_diff
returns
[1] 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
Is there a more efficient way? I want to scale this up to thousands (millions?) of both obj1 & obj2.
Thanks, Aaron