Re: Is there a better way? [message #9162] |
Wed, 11 June 1997 00:00  |
Stein Vidar Hagfors H
Messages: 32 Registered: May 1997
|
Member |
|
|
Kelly Dean wrote:
>
> Is there a better way to locate a value in a table (or array)? I would
> like to come in with an array of radaince values and convert them to
> temperatures. The method below only takes the one number (rad) then
> search for the best match in the table (or array).
>
> If you can thing of a better method, send me a note.
Well, I can suggest a couple of things that will probably
speed up the routine considerably, though I can't guarantee
that it's the *best* way...
> =================================================
> FUNCTION IS_THERE_A_BETTER_WAY, rad
>
If the table you're using is actually created this way,
I'd use a common block as a cache to avoid initializing
the tables on each call, e.g.:
COMMON IS_THERE_A_BETTER_WAY_CACHE,WVTEMParr,WVRADarr
IF n_elements(WVTEMParr) eq 0 THEN BEGIN
<initialize arrays here>
ENDIF
Instead of looping etc, I'd put my money on IDL's array processing
capabilities:
absdiff = abs(WVRADarr - rad)
dummy = min(absdiff,minix) ; Minix now contains the index of
; the point with the minimum abs diff.
return,WVTEMParr(minix)
> END
Unless your real arrays are very large, this is probably
faster than a search in a sorted array, due to the slow
execution of single statements vs array operations.
A native IDL routine doing a sorted search should be
available, though.... haven't found one yet....
Stein Vidar
|
|
|