If you know that your values are sorted, you could use I. to get an initial approximation (a set of up to three values) and then use brute force to pick from them.
This assumes also that you want a single value (or index). This also winds up being more code than the other proposals I've seen here. So I'm not sure if it's "worth it". -- Raul On Mon, Jun 1, 2015 at 9:02 PM, Joe Bogner <[email protected]> wrote: > Is there a more efficient, idiomatic method of finding the nearest > (absolute difference) x to y? > > nearest=:([ {~ {.@/: @: |@:-) > > 1 5 8 9 nearest 4 > 5 > > 1 5 8 9 nearest 6 > 5 > > 1 5 8 9 nearest 5 > 5 > > 1 5 8 9 nearest 2 > 1 > > This seems to work fine but was curious if there is a more common solution > ---------------------------------------------------------------------- > For information about J forums see http://www.jsoftware.com/forums.htm ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
