Path: news.mathworks.com!not-for-mail
From: "Sven " <sven.holcombe@removethis.gmail.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Find nearest index
Date: Tue, 31 Jul 2007 07:06:46 +0000 (UTC)
Organization: Japan Automobile Research Institute
Lines: 41
Message-ID: <f8mn26$nk1$1@fred.mathworks.com>
References: <f8mjkc$m3i$1@fred.mathworks.com> <f8mk7i$o4r$1@canopus.cc.umanitoba.ca>
Reply-To: "Sven " <sven.holcombe@removethis.gmail.com>
NNTP-Posting-Host: webapp-01-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1185865606 24193 172.30.248.36 (31 Jul 2007 07:06:46 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 31 Jul 2007 07:06:46 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 521344
Xref: news.mathworks.com comp.soft-sys.matlab:421807


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.