findIndexInOrderedV​ector

Finds the first or last index of a SORTED vector that satisfies some (in)equality constraint with a value using binary search *FAST*

You are now following this Submission

Index = findIndexInOrderedVector(SearchVector, operation, value, whichelement)

The output of the function is similar to certain uses of MATLAB's find() function, but is done much faster for large vectors by using binary search. An example is:
Index = find(SearchVector >= 3, 1);

This would be:
Index = findIndexInOrderedVector(SearchVector, 'geq', 3, 'first');

SearchVector is a sorted vector either ascending or descending.
operation can be equality, inequality, greater than, greater than or equal, less than, less than or equal.
value is a scalar value.
whichelement can be first or last.

As long as the operations of ==, >=, >, <=, < are available for the data type of the vector, the function should work, i.e. it can work with e.g. datetime vectors.

The function makes no assumption that the element exists. If it does not exist, it returns an empty element, i.e. isempty(Index) == true holds.

Enjoy. There may be bugs. Feel free to fix them and upload.

Cite As

Konstantinos Georgiadis (2026). findIndexInOrderedVector (https://in.mathworks.com/matlabcentral/fileexchange/69864-findindexinorderedvector), MATLAB Central File Exchange. Retrieved .

General Information

MATLAB Release Compatibility

  • Compatible with any release

Platform Compatibility

  • Windows
  • macOS
  • Linux
Version Published Release Notes Action
1.0.1

fixed bug.

1.0.0