Finding the closest number from a list
- To: mathgroup at smc.vnet.net
- Subject: [mg39510] Finding the closest number from a list
- From: jrome at mail.com (Jacob Rome)
- Date: Thu, 20 Feb 2003 05:14:29 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
Hi, I have a seemingly simple problem. I want to find the element in a list that is closest to a number I specify. In my case, I have a list of about 30,000 numbers (y[i]). I want to find the closest match for each of about 10,000 other numbers (call them x[i]). Obviously, speed is important. I've sorted the large list, and right now I'm going through each y[i] from lowest to highest and testing it to see if x[i] is less than that value. This takes about .1 seconds for each x[i]. I'm wondering if anyone has had a similar problem, and if there is a better function built-in to Mathematica. Alternatetively, I could build my own. I've just recently realized that I could also reduce the search time considerably if I sort the x[i] list as well, and only start my search from where I last left off. Any ideas on which approach would be more efficient? Thanks.
- Follow-Ups:
- Re: Finding the closest number from a list
- From: Daniel Lichtblau <danl@wolfram.com>
- Re: Finding the closest number from a list