[Date Index] [Thread Index] [Author Index]
Finding the closest number from a list
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.