comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » search routine
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Return to the default flat view Create a new topic Submit Reply
search routine [message #54247] Fri, 01 June 2007 03:47 Go to previous message
Laurens is currently offline  Laurens
Messages: 41
Registered: May 2006
Member
Hi folks,

From Martin Schultz (posted in 1999) I found the following array-search
algorithm which seems to do a fine job.
Except that i'm not able to catch the first element in the array.

Example:

Array = [0,80,100,120,180,300]
result = search, Array, 4.53

It should return index 0, if I understand it correctly, but it returns 1
instead. Now I don't quite follow the logic of the function, so maybe
someone for which it's easy to see can help me in the right direction?

> function search,data,value
>
> ; search first occurence of value in data set
> ; data must be sorted
>
> ; simple error checking on data and value
> if (n_elements(value) eq 0) then begin
> message,'Must supply sorted data array and value),/CONT
> return
> endif
>
> ndat = n_elements(data)
>
> try = fix(0.5*ndat)
> step = 0.5*try
>
> ; find index of nearest points
> while (step gt 1) do begin
> if (data[try] gt value) then $
> try = try-step $
> else $
> try = try+step
> step = fix(0.5*(step+1))
> endwhile
>
> ; now get the data point closest to value
> ; can only be one out of three (try-1, try, try+1)
> dummy = min( abs(value-data[try-1:try+1]), location )
>
> return,try+location-1
>
> end

Thnx! Laurens
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Previous Topic: Plots
Next Topic: Thanks

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Sun Oct 12 09:37:26 PDT 2025

Total time taken to generate the page: 1.35957 seconds