Search closest value in a vector
by Dr. Murtaza Khan
23 May 2007
(Updated 09 Jul 2009)
search value in sorted vector and find index and value with respect to vector that is equal or clos
|
Watch this File
|
| File Information |
| Description |
Search value 'v' in sorted vector 'x' and find index and value
with respect to vector x that is equal or closest to 'v'.
If more than one value is equal then anyone can be returned
(this is property of binary search).
If more than one value is closest then first occurred is returned
(this is property of linear search).
Algorithm
First binary search is used to find v in x. If not found
then range obtained by binary search is searched linearly
to find the closest value.
INPUT:
x: vector of numeric values,
x should already be sorted in ascending order
(e.g. 2,7,20,...120)
v: numeric value to be search in x
OUTPUT:
i: index of v with respect to x.
cv: value that is equal or closest to v in x |
| MATLAB release |
MATLAB 7.0.4 (R14SP2)
|
|
Tags for This File
|
| Everyone's Tags |
|
| Tags I've Applied |
|
| Add New Tags |
Please login to tag files.
|
| Comments and Ratings (8) |
| 24 May 2007 |
Scott Miller
|
|
|
| 25 May 2007 |
James J Cai
|
|
|
| 26 May 2007 |
Dimitri Shvorob
|
|
|
| 26 May 2007 |
M Ali
|
|
|
| 29 May 2007 |
Dimitri Shvorob
|
|
|
| 07 Jul 2007 |
. ..
|
|
|
| 18 Mar 2008 |
Schneider Huetter
|
|
|
| 03 Apr 2008 |
akash varma
|
|
|
| Updates |
| 09 Jul 2009 |
BSD License |
|
Contact us at files@mathworks.com