1
$\begingroup$

For versions prior to 10.1 the following returns a error message

timedat = {1.1, 2, 3.3};
equalTime = {1, 2, 3};
Flatten@Nearest[timedat, equalTime]

Nearest::dmtch: The dimension of {1.1,2,3.3} and {1,2,3} does not match. >>

A workaround is

Flatten[Nearest[timedat, #] & /@ equalTime]
(* {1.1, 2, 3.3}*)

However, I was thinking if there is a way to modify the default behavior of Nearest, for versions prior to 10.1, in order to achieve the better performance of the current version.

That is,

Flatten@Nearest[timedat, equalTime]

should return

{1.1, 2, 3.3}
$\endgroup$
  • 1
    $\begingroup$ Flatten@With[{n = Nearest[#1]}, n /@ #2] &[timedat[[All, 1]], equalTime] $\endgroup$ – ciao Sep 25 '15 at 9:23
  • 2
    $\begingroup$ myNearest[l1_List, l2_List] := With[{n = Nearest@l1}, n /@ l2] if you want it as a generalized function. You could unprotect Nearest and overload it, but generally, that's a seriously bad idea... $\endgroup$ – ciao Sep 25 '15 at 9:30
  • 1
    $\begingroup$ @ciao generalize that to pass through additional arguments and make it an answer. $\endgroup$ – george2079 Sep 25 '15 at 12:07
  • $\begingroup$ @george2079: feel free to - this strikes me as bordering on RTM... $\endgroup$ – ciao Sep 26 '15 at 0:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.