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

Home » Public Forums » archive » Re: Looking for a 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
Re: Looking for a search routine [message #5260 is a reply to message #5254] Fri, 24 November 1995 00:00 Go to previous message
Frank J. �ynes is currently offline  Frank J. �ynes
Messages: 17
Registered: February 1995
Junior Member
jvkepner@airy.Princeton.EDU (Jeremy Kepner) wrote:
> I have two 1D floating point vectors X and Y.
> Y contains values sorted in increasing order. I am
> looking for a function that for each element in the
> X array, X(i), will return the index of the element in the
> Y array that is nearest to X(i). Ideally it would
> be a function that would look something like
>
> y_ids = SEARCH(X,Y)
>
> -Jeremy Kepner
> Dept. of Astrophysics
> Princeton University

You'll probably have to do it element by element.
Maybe the quickest algo. would be a binary searc in
the y array. Still, a quick and dirty approach whould be
something like:
FUNCTION Search, x, y
n_elem = N_ELEMENTS(x)
ndx = lonarr(n_elem)

FOR i = 0, n_elem -1 DO BEGIN
diff = abs(y - x(i))
ndx(i) = (where (diff EQ min(diff)))(0)
ENDFOR

RETURN, ndx
END

This function does not require Y to be sorted.

Better suggestions, IDL hackers ?



--
/* Frank J. �ynes | frank@spacetec.no /*
/* Spacetec a.s | Phone: +47 77684500 Fax: +47 77655859 /*
/* Prestvannv. 38, | /*
/* N-9005 Troms�, Norway | (...with the bravery of being out of range!) /*
[Message index]
 
Read Message
Read Message
Previous Topic: Read Hex numbers
Next Topic: Blanking out section of contour plot

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

Current Time: Sat Oct 11 22:08:28 PDT 2025

Total time taken to generate the page: 1.03739 seconds