The IDL of Brian
Round a value to its nearest element in an array.
7Jan2008 Update: I totally rewrote this to use value_locate which gave a factor of 11 speedup based on one test. Note that to match value_locate I changed the order of the inputs, sorry.
It never fails that you need to do this, especially when you are using cursor. I finally got fed up with hacking this together each time and wrote this. I bet it can be improved and perhaps even use histogram, but there are too many gremlins in there for now.
Download: round2array.pro
I will give an example here.
Setup an array of values to find that I want to match
IDL> print, findgen(10)+0.2
0.200000 1.20000 2.20000 3.20000 4.20000 5.20000 6.20000 7.20000 8.20000 9.20000
print the indices that are the closest elements to my input
IDL> print, round2array(findgen(10)+0.2, [3.4, 3.0, 6.7, 6.3])
3 3 7 6
and print the values that are the closest
IDL> print, (findgen(10)+0.2)[round2array(findgen(10)+0.2), [3.4, 3.0, 6.7, 6.3]]
3.20000 3.20000 7.20000 6.20000
Monday, January 7, 2008
round2array (updated 7Jan2008)