help-gsl
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Help-gsl] Getting the index of a sorted vector entry closest to a certa


From: Luke
Subject: [Help-gsl] Getting the index of a sorted vector entry closest to a certain value
Date: Tue, 8 Jun 2010 13:31:29 -0700

I have a gsl_vector whose values are evenly space from -M_PI to M_PI.
I am using the GSL single dimensional root finding algorithm on a
single equation which gives me a value that is in the range of [-M_PI,
M_PI], but won't be exactly equal to any of the elements.  My goal is
to find the index of the element in my gsl_vector which is closest to
the solution from the root finding algorithm.  Here is an illustration
of situation:

v = {-1.0, -0.5, 0.0, 0.5, 1.0}
x_star = 0.3

Since the entry of v closest to x_star is 0.5, the index I would want
to obtain is i = 3.

I would imagine the function call to be something like:

i = get_nearest_index(v, x_star);

I can easily see how I would use a for loop from i = 0 to N, but I was
wondering if perhaps this code has already been implemented within GSL
more efficiently using other searching algorithms, like some sort of
binary search or something.

Also, is there a function already in GSL which generates evenly space
vectors?  A C equivalent of the Octave/Matlab v = 0:0.01:1 or
linspace(0, 1, 101).  If not, is there interest in these being
implemented and included in the gsl_vector library?

Thanks,
~Luke



reply via email to

[Prev in Thread] Current Thread [Next in Thread]