Path: news.mathworks.com!not-for-mail
From: "Sven " <sven.holcombe@removethis.gmail.com>
Newsgroups: comp.soft-sys.matlab
Subject: Find nearest index
Date: Tue, 31 Jul 2007 06:08:12 +0000 (UTC)
Organization: Japan Automobile Research Institute
Lines: 15
Message-ID: <f8mjkc$m3i$1@fred.mathworks.com>
Reply-To: "Sven " <sven.holcombe@removethis.gmail.com>
NNTP-Posting-Host: webapp-00-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1185862092 22642 172.30.248.35 (31 Jul 2007 06:08:12 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 31 Jul 2007 06:08:12 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 521344
Xref: news.mathworks.com comp.soft-sys.matlab:421800


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.