From: "Alistair Templeton" <>
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$>
References: <>
Reply-To: "Alistair Templeton" <>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: 1303490465 28348 (22 Apr 2011 16:41:05 GMT)
NNTP-Posting-Date: Fri, 22 Apr 2011 16:41:05 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1897919
Xref: 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: