binarySearchClosest
Version 1.0.0 (1,76 KB) von
Joshua Bauske
Find the closest value to a target in a sorted array using binary search
This function performs a binary search to find the closest value to a specified target in a sorted array. In case of a direct match the index of the matching value is returned. If the searchValue is not included in the array either the idx of the first/smallest or the idx of the last/largest value is returned.
Worst-case performance: O(log n)
Best-case performance: O(1)
Usage Example
a = 1:10;
binarySearchClosest(a, -1); % = 1
binarySearchClosest(a, 5); % = 5
binarySearchClosest(a, 9.51); % = 10
binarySearchClosest(a, 11); % = 10
Zitieren als
Joshua Bauske (2025). binarySearchClosest (https://de.mathworks.com/matlabcentral/fileexchange/182197-binarysearchclosest), MATLAB Central File Exchange. Abgerufen.
Kompatibilität der MATLAB-Version
Erstellt mit
R2025b
Kompatibel mit allen Versionen
Plattform-Kompatibilität
Windows macOS LinuxTags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Live Editor erkunden
Erstellen Sie Skripte mit Code, Ausgabe und formatiertem Text in einem einzigen ausführbaren Dokument.
Version | Veröffentlicht | Versionshinweise | |
---|---|---|---|
1.0.0 |