Hmmm, what about 100? That is, which answer do you want in the
case that something is equidistant between two numbers?
Also, which ordering should be assumed if the input has
complex numbers? Matlab uses two different orderings
for complex numbers; the ordering used for sort() is
different than for several other operations.
Are we allowed to assume that there will be at most one
negative infinity and at most one positive infinity?
And that there will be no NaN's, since NaN's are,
by definition, not numbers and the array was said
to contain numbers?
--
All is vanity. -- Ecclesiastes
roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in message <f8mk7i$o4r$1@canopus.cc.umanitoba.ca>...
> In article <f8mjkc$m3i$1@fred.mathworks.com>,
> Sven <sven.holcombe@removethis.gmail.com> wrote:
>
> >Given an array of increasing numbers, how do I find the nearest index to another given number?
>
> >Ie,
> >myCol = [50 150 200]
> >nearestNeigbour(myCol, 40) % returns 1
> >nearestNeigbour(myCol, 65) % returns 1
> >nearestNeigbour(myCol, 130) % returns 2
>
> Hmmm, what about 100? That is, which answer do you want in the
> case that something is equidistant between two numbers?
In this case 100 should return 2. I think this matches the behaviour of 'round', except that whereas round(value) rounds to the nearest integer, nearestNeighbour(values, value) essentially rounds to the nearest item in the set of values (returning its index).
> Also, which ordering should be assumed if the input has
> complex numbers? Matlab uses two different orderings
> for complex numbers; the ordering used for sort() is
> different than for several other operations.
I hadn't considered complex numbers at all really.
> Are we allowed to assume that there will be at most one
> negative infinity and at most one positive infinity?
> And that there will be no NaN's, since NaN's are,
> by definition, not numbers and the array was said
> to contain numbers?
I would expect that any number outside the range of values given would return 1 or length(values).
To give some more context, I am running this on a set of numbers after making some bins using the hist function.
The hist function returns the centres of each bin, and I would now like to loop over my original set of numbers and find out which bin it belongs in.
I'm sure I could write a short function that does what I'm looking for, but please let me know if it seems I'm reinventing a wheel long available.
"Sven " <sven.holcombe@removethis.gmail.com> wrote in message <f8mjkc$m3i$1@fred.mathworks.com>...
> Hi there, this should be a real simple one.
>
> Given an array of increasing numbers, how do I find the nearest index to another given number?
>
> Ie,
> myCol = [50 150 200]
> nearestNeigbour(myCol, 40) % returns 1
> nearestNeigbour(myCol, 65) % returns 1
> nearestNeigbour(myCol, 130) % returns 2
> nearestNeigbour(myCol, 201) % returns 3
>
> I could write this function myself, but I imagine that it already exists in some form and I just haven't been able to find it.
>
Dear Sven,
sometimes I use the following command to do things like this:
givennumber=201;
[val,ind] = min(abs(myCol-givennumber));
> Dear Sven,
>
> sometimes I use the following command to do things like this:
> givennumber=201;
> [val,ind] = min(abs(myCol-givennumber));
>
> I hope i got it right,
> best wishes,
> Kris
>
>
Ups, too slow.
"Kris Miller" <kris@nomail.com> wrote in message <f8mnng$6an$1@fred.mathworks.com>...
> ...
>
> > Dear Sven,
> >
> > sometimes I use the following command to do things like this:
> > givennumber=201;
> > [val,ind] = min(abs(myCol-givennumber));
> >
> > I hope i got it right,
> > best wishes,
> > Kris
> >
> >
> Ups, too slow.
>
> Kris
>
ind = interp1(myCol, 1:length(myCol), [40 65 130 201], 'nearest', 'extrap')
Titus
"Sven " <sven.holcombe@removethis.gmail.com> schrieb im Newsbeitrag
news:f8mjkc$m3i$1@fred.mathworks.com...
> Hi there, this should be a real simple one.
>
> Given an array of increasing numbers, how do I find the nearest index to
> another given number?
>
> Ie,
> myCol = [50 150 200]
> nearestNeigbour(myCol, 40) % returns 1
> nearestNeigbour(myCol, 65) % returns 1
> nearestNeigbour(myCol, 130) % returns 2
> nearestNeigbour(myCol, 201) % returns 3
>
> I could write this function myself, but I imagine that it already exists
> in some form and I just haven't been able to find it.
>
> Cheers,
> Sven.
"us " <us@neurol.unizh.ch> wrote in message <f8n5ki$k3u$1@fred.mathworks.com>...
> Sven:
> <SNIP in search of proximity...
>
> one of the many other solutions
>
> % some data
> n=[50 150 200];
> m=[40 65 130 201 -10 199];
> % the engine
> [ix,ix]=min(abs(bsxfun(@minus,m,n.')));
> % the result
> ix
>
> us
I just tested this a bit and it seems a standard bisection search seems to be a lot faster if n (and, to less extent, m) are large...
On Jul 31, 9:06 am, "Sven " <sven.holco...@removethis.gmail.com>
wrote:
> To give some more context, I am running this on a set of numbers after making some bins using the hist function.
> The hist function returns the centres of each bin, and I would now like to loop over my original set of numbers and find out which bin it belongs in.
>
> Thanks,
> Sven.
Dear Sven,
you can use histc with two lhs arguments: [n, bins] = histc(x);
In bins you get an vector with the indices.
Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for
all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content.
Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available
via MATLAB Central. Read the complete Disclaimer prior to use.