help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: bsearch in octave


From: LUK ShunTim
Subject: Re: bsearch in octave
Date: Sat, 25 Jun 2005 14:09:08 +0800
User-agent: Mozilla Thunderbird 1.0.2 (Windows/20050317)

On 2005-06-25 1:00, Jeff Abrahamson wrote:
I want to find the last element in a row vector that is less than a
given value.  I want the index of this value.  In C, for example, I'd
use bsearch (binary search), thus the subject on my mail.

I don't see how to do this in octave (besides implementing binary
search).  Did I miss it?  Any pointers?

Thanks much.

For example,

    bsearch([1, 3, 7, 15], 6)  ==> 2
    bsearch([1, 3, 7, 15], 7)  ==> 3
    bsearch([1, 3, 7, 15], 8)  ==> 3

That is, 3 < 6 < 7, but 7 < 7 < 15 and 7 < 8 < 15.  And 3 has index 2,
7 has index 3.


This seems to be what you want.

a=[1,3,7,15];
index=find(a>6)(1) - 1
index = 2
index=find(a>7)(1) - 1
index = 3
index=find(a>8)(1) - 1
index = 3

Regards,
ST
--



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------



reply via email to

[Prev in Thread] Current Thread [Next in Thread]