Function used in binary search, MATLAB in Engineering

Function used in binary search:

The function below implements this binary search algorithm. It receives two arguments: the sorted vector and a key (on the other hand, the function could sort the vector). The value of low and high are initialized to the first and last indices in the vector. The outind that is the output argument is initialized to -1 that is the value which the function will return when the key is not found. It loops until either low is greater than high, or til the key is found.

345_Function used in binary search.png

The illustrations of calling this function are as shown here:

>> vec = randint(1,7, [1 30])

vec =

2 11 25 1 5  7  6

>> svec = sort(vec)

svec =

1 2  5 6 7 11 25

>> binsearch(svec, 4)

ans =

    -1

>> binsearch(svec, 25)

ans =

    7

>> binsearch(svec, 5)

ans =

    3

Posted Date: 10/22/2012 8:03:05 AM | Location : United States







Related Discussions:- Function used in binary search, Assignment Help, Ask Question on Function used in binary search, Get Answer, Expert's Help, Function used in binary search Discussions

Write discussion on Function used in binary search
Your posts are moderated
Related Questions
Changing Case: The MATLAB has two functions which convert strings to all uppercase letters, or all lowercase, known as the upper and lower. >> mystring = 'AbCDEfgh';

Patch function - graphics objects: The patch function is used to generate a patch graphics object, which is made from 2-dimensional polygons. The patch is defined by its verti

Finding products by for loop: an illustration, when 5 is passed to be the value of the input argument n, the function will compute and return 1 + 2 + 3 + 4 + 5, or 15: >> s

Reading from a File in a While Loop: Though in most languages the combination of a loop and an if statement would be essential to determine whether or not the elements in a ve

Application: Menu driven Modular Program Numerous longer, more involved programs which have interaction with the user are menu-driven, that means that the program prints a men

Vector operations: As vectors are special cases of matrices, the matrix operations elaborated (addition, subtraction, multiplication, scalar multiplication, transpose) work on

Polyhedron - graphics objects: The field polyhedron.vertices is a matrix in which each row presents (x,y,z) points. The field polyhedron.faces defines the faces: for illustrat

Replacement : Replace a row by adding it to (or subtract from it) a multiple of the other row. For a given row ri, this is written as   ri  - srj →  ri Note that when r

I have a frequency response data. How do I convert that to state space? I am given a 6 row and 3 column data (steady state). How do i convert that to state space model?

For Loops which do not use an iterator Variable in the action: In all the illustrations that we seen so far, the value of the loop variable has been used in same way in the ac