Path: news.mathworks.com!newsfeed-00.mathworks.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: Bruce Bowler <bbowler@bigelow.org>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Which key is closest?
Date: 25 Apr 2011 13:53:37 GMT
Lines: 33
Message-ID: <91lcn0Faa4U1@mid.individual.net>
References: <91dksaFikoU1@mid.individual.net>
	<iot1jg$5v5$1@fred.mathworks.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: individual.net P8twe/LUxQfPoiLr/j6aQgonUbXdOVB9wDO+BqFAzTHOEF63AG
Cancel-Lock: sha1:MH99NYgwQZpPNfJS19OZmqL8bVw=
User-Agent: Pan/0.133 (House of Butterflies)
Xref: news.mathworks.com comp.soft-sys.matlab:723683

On Fri, 22 Apr 2011 23:05:20 +0000, Roger Stafford set fingers to keyboard
and typed:

> 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

Dat's da one!  Thanks Roger!

Bruce