Fast searching for the lowest value greater than x in a sorted vector

Viewed 2490

Fast means better than O(N), which is as good as find() is capable of. I know there is ismembc and ismembc2, but I don't think either of them are what I am looking for. I read the documentation and it seems they search for a member equal to x, but I want the index of first value greater than x.

Now if either of these functions is capable of doing this, could somebody please give an example, because I can't figure it out.

Ideal behaviour:

first_greater_than([0, 3, 3, 4, 7], 1)

returns 2, the index of the first value greater than 1, though obviously the input array would be vastly larger.

Of course, a binary search isn't too difficult to implement, but if MATLAB has already done it, I would rather use their method.

2 Answers
Related