Re: Efficient search for bounding list elements
- To: mathgroup at smc.vnet.net
- Subject: [mg114192] Re: Efficient search for bounding list elements
- From: Alexei Boulbitch <alexei.boulbitch at iee.lu>
- Date: Fri, 26 Nov 2010 05:31:01 -0500 (EST)
Hi, David, this searchFunction[x_, a_] := {Last@Select[a, #< x&], First@Select[a, #> x&]} may give the answer you are looking for. Let us try: a = Range[1000, 5000, 250]; x = 1800; searchFunction[x, a] {1750, 2000} Have fun, Alexei I've been looking for a way to efficiently find the indices of the two elements of a monotonically increasing list which bound a number. For example, if a = Range[1000,5000,250] and x=1600 then I'd like this function (e.g., searchFunction[x,a]) to return {3,4}, which correspond to the 3rd and 4th elements of a, which are 1500 and 1750, respectively. I can easily do this in a loop, but in my application a can be very large (hundreds of thousands or millions of elements) and this operation needs to repeated thousands of times, so efficiency is critical. Any suggestions would be greatly appreciated! Thanks, David Skulsky -- Alexei Boulbitch, Dr. habil. Senior Scientist Material Development IEE S.A. ZAE Weiergewan 11, rue Edmond Reuter L-5326 CONTERN Luxembourg Tel: +352 2454 2566 Fax: +352 2454 3566 Mobile: +49 (0) 151 52 40 66 44 e-mail: alexei.boulbitch at iee.lu www.iee.lu -- This e-mail may contain trade secrets or privileged, undisclosed or otherwise confidential information. If you are not the intended recipient and have received this e-mail in error, you are hereby notified that any review, copying or distribution of it is strictly prohibited. Please inform us immediately and destroy the original transmittal from your system. Thank you for your co-operation.