|
|
| File Information |
| Description |
Find the value (wert) in the in ascending order sorted array (array) with
fastsearch-algorithm.
function index = fastsearch (array, von, bis, wert, bias)
Input
array: array to search in
von: lower border of the search region, should be set to 1.
bis: higher border of the search region, should be set to
length(array). These variables cannot be initialize within the
function because of the recursion.
wert: element to search for
bias: 0: search exact the element
+1: search the element or the next greater
-1: search the element or the next smaller
Output
index: index of the element in array;
-1 if not found
The algorithm is very quick, check the time below:
--------------------
>> a=rand(1,10^7);
b=sort(a);
tic;c=fastsearch(b,1,length(b),b(123456),1);toc
Elapsed time is 0.000795 seconds.
>> c
c =
123456
--------------------
>> tic;c=fastsearch(b,1,length(b),0.16234,-1);toc
Elapsed time is 0.014674 seconds.
>> 0.16234-b(c)
ans =
7.6316e-008
>> 0.16234-b(c+1)
ans =
-7.0494e-008 |
| MATLAB release |
MATLAB 7.4 (R2007a)
|
|
Tags for This File
|
| Everyone's Tags |
|
| Tags I've Applied |
|
| Add New Tags |
Please login to tag files.
|
| Comments and Ratings (8) |
| 18 Oct 2007 |
Yi Cao
|
|
|
| 19 Oct 2007 |
urs (us) schwarz
|
|
|
| 19 Oct 2007 |
urs (us) schwarz
|
|
|
| 05 May 2008 |
Alexander Chemeris
|
|
|
| 06 May 2008 |
Valentin Kuklin
|
|
|
| 09 Nov 2008 |
Dru Franworth
|
|
|
| 10 Nov 2008 |
Dru Franworth
|
|
|
| 10 Nov 2008 |
Dru Franworth
|
|
|
| Updates |
| 12 May 2008 |
The improvement is recommended by Urs (us) Schwarz.
The function is called now as
fastsearch (array, wert, bias). |
| 09 Dec 2008 |
The bug noticed by Dru Franworth (thanks) is fixed. |
|
Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for
all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content.
Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available
via MATLAB Central.
Read the complete Disclaimer prior to use.
Contact us at files@mathworks.com