Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: Find nearest index

Subject: Find nearest index

From: Sven

Date: 31 Jul, 2007 06:08:12

Message: 1 of 11

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.

Subject: Re: Find nearest index

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 31 Jul, 2007 06:18:26

Message: 2 of 11

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?

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

Subject: Re: Find nearest index

From: Daniel Walker

Date: 31 Jul, 2007 07:05:30

Message: 3 of 11

... "quick & dirty"

val = 201;
[t_delta, t_ind] = min(abs(myCol-val));

Cheers
D.

Subject: Re: Find nearest index

From: Sven

Date: 31 Jul, 2007 07:06:46

Message: 4 of 11

Thanks for the prompt input.

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.

Thanks,
Sven.

Subject: Re: Find nearest index

From: Kris Miller

Date: 31 Jul, 2007 07:14:20

Message: 5 of 11

"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));

I hope i got it right,
best wishes,
Kris



Subject: Re: Find nearest index

From: Kris Miller

Date: 31 Jul, 2007 07:18:08

Message: 6 of 11

...
 
> 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

Subject: Re: Find nearest index

From: Sven

Date: 31 Jul, 2007 07:31:48

Message: 7 of 11

"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
>

Thanks all, very helpful.

Cheers,
Sven.

Subject: Re: Find nearest index

From: Titus

Date: 31 Jul, 2007 12:47:30

Message: 8 of 11

Hi Sven,
what about

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.


Subject: Re: Find nearest index

From: us

Date: 31 Jul, 2007 11:15:31

Message: 9 of 11

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

Subject: Re: Find nearest index

From: John Vendt

Date: 31 Jul, 2007 19:03:41

Message: 10 of 11

"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...

Subject: Re: Find nearest index

From: ralphs

Date: 01 Aug, 2007 01:07:27

Message: 11 of 11

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.

HTH.
Ralph

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
indexing Ned Gulley 31 Jul, 2007 21:52:01
distance us 31 Jul, 2007 07:20:05
min us 31 Jul, 2007 07:20:05
bsxfun us 31 Jul, 2007 07:20:04
code us 31 Jul, 2007 07:20:04
rssFeed for this Thread

envelope graphic E-mail this page to a colleague

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.
Related Topics