1

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}
Dr. belisarius
  • 115,881
  • 13
  • 203
  • 453
Dimitris
  • 4,794
  • 22
  • 50
  • 1
    Flatten@With[{n = Nearest[#1]}, n /@ #2] &[timedat[[All, 1]], equalTime] – ciao Sep 25 '15 at 09:23
  • 2
    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... – ciao Sep 25 '15 at 09:30
  • 1
    @ciao generalize that to pass through additional arguments and make it an answer. – george2079 Sep 25 '15 at 12:07
  • @george2079: feel free to - this strikes me as bordering on RTM... – ciao Sep 26 '15 at 00:52

0 Answers0