Path: news.mathworks.com!not-for-mail
From: "Kris Miller" <kris@nomail.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Find nearest index
Date: Tue, 31 Jul 2007 07:14:20 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 27
Message-ID: <f8mngc$20a$1@fred.mathworks.com>
References: <f8mjkc$m3i$1@fred.mathworks.com>
Reply-To: "Kris Miller" <kris@nomail.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 1185866060 2058 172.30.248.36 (31 Jul 2007 07:14:20 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 31 Jul 2007 07:14:20 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1070232
Xref: news.mathworks.com comp.soft-sys.matlab:421811


"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