From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Which key is closest?
Date: Fri, 22 Apr 2011 23:05:20 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 19
Message-ID: <iot1jg$5v5$>
References: <>
Reply-To: <HIDDEN>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: 1303513520 6117 (22 Apr 2011 23:05:20 GMT)
NNTP-Posting-Date: Fri, 22 Apr 2011 23:05:20 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: comp.soft-sys.matlab:723385

Bruce Bowler <> wrote in message <>...
> I'm having a brain fart this morning...
> 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). 
> I seem to recall a method that uses HISTC but my pea sized brain can't 
> figure it out.  
> Any help greatly appreciated.
> Thanks,
> Bruce
- - - - - - - - -
  Yes, by all means use 'histc' to save time searching.  I believe it performs some kind of binary search.  Use [-inf,KEY,+inf] as the 'edges' argument and 'NEWKEY' as the single 'x' argument.  It is the second returned argument you want to use to locate your position.  You will have to do one extra step to arrive at your closest location.  Be sure to read the documentation for it carefully.

Roger Stafford