Path: news.mathworks.com!not-for-mail
From: "Alistair Templeton" <bigalt2000@yahoo.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Which key is closest?
Date: Fri, 22 Apr 2011 16:41:05 +0000 (UTC)
Organization: Rush University
Lines: 11
Message-ID: <iosb30$rls$1@fred.mathworks.com>
References: <91dksaFikoU1@mid.individual.net>
Reply-To: "Alistair Templeton" <bigalt2000@yahoo.com>
NNTP-Posting-Host: www-05-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1303490465 28348 172.30.248.37 (22 Apr 2011 16:41:05 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Fri, 22 Apr 2011 16:41:05 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1897919
Xref: news.mathworks.com comp.soft-sys.matlab:723335


> I have a vector of numbers which are sorted in ascending order, KEY, and 
> another single value, NEWKEY.  NEWKEY may or may not be present in KEY.  I 
> need to find the index in KEY whose value is the same as (easy) or the one 
> closest to but less than NEWKEY (where I'm stuck, short of a loop). 

interp1 gets you close, but you can only find the closest without rounding up.

here's a non-robust, obnoxious way to play with:

key(find(diff(key>=newkey))+1)