Re: Finding the closest number from a list
- To: mathgroup at smc.vnet.net
- Subject: [mg39562] Re: Finding the closest number from a list
- From: Bill Rowe <listuser at earthlink.net>
- Date: Sun, 23 Feb 2003 05:00:29 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
On 2/22/03 at 3:38 AM, drbob at bigfoot.com (Dr Bob) wrote: >I think this works just as well, and a little faster: > >closest[list_?VectorQ, things_?VectorQ] := Module[{sorted, lint, >first, last, n = Length[list], lower, upper}, \ sorted = Sort[list]; >first = Max[Min[things], First[sorted]] - 1; last = Min[Max[things], >Last[sorted]] + 1; lint = ListInterpolation[Join[{1}, Range[n], {n}], >{ Join[{first}, sorted, {last}]}, InterpolationOrder -> 1]; >list[[Round@lint@things]] >] I think that last line of code should be sorted[[Round@lint@things]]