findIndexInOrderedVector
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://www.mathworks.com/matlabcentral/fileexchange/69864-findindexinorderedvector), MATLAB Central File Exchange. Retrieved .
MATLAB Release Compatibility
Platform Compatibility
Windows macOS LinuxCategories
Tags
Discover Live Editor
Create scripts with code, output, and formatted text in a single executable document.
