Re: Looking for a search routine [message #5260 is a reply to message #5254] |
Fri, 24 November 1995 00:00  |
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!) /*
|
|
|