searchsorted

Binary search in the style of numpy's searchsorted
30 Downloads
Updated 15 Apr 2024

View License

Finds indices in sorted vector x where values would be inserted to maintain sorted order: in style of numpy searchsorted.
indices = searchsorted(x, values)
name value arguments (nv):
'side' (default: 'left')
e.g., side='left': x = [1, 3, 5, 5, 7], values = [2, 5, 7] -> indices = [2, 3, 5]
side='right': x = [1, 3, 5, 5, 7], values = [2, 5, 7] -> indices = [2, 4, 5]
'sortfirst' (default: false)
if true, x will be sorted prior to the search.
Credit: DrGar
(https://stackoverflow.com/questions/20166847/faster-version-of-find-for-sorted-vectors-matlab)
Amended by Luke Jenkins, Apr 2024

Cite As

Luke Jenkins (2026). searchsorted (https://se.mathworks.com/matlabcentral/fileexchange/163511-searchsorted), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2024a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Version Published Release Notes
1.0.0