Path: news.mathworks.com!not-for-mail
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$1@fred.mathworks.com>
References: <91dksaFikoU1@mid.individual.net>
Reply-To: <HIDDEN>
NNTP-Posting-Host: www-00-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1303513520 6117 172.30.248.45 (22 Apr 2011 23:05:20 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Fri, 22 Apr 2011 23:05:20 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: news.mathworks.com comp.soft-sys.matlab:723385

Bruce Bowler <bbowler@bigelow.org> wrote in message <91dksaFikoU1@mid.individual.net>...
> 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