Min/Max selection

Search for k smallest or largest elements in the array
Updated 20 May 2016

View License

Using a partial quick-sort algorithm implemented with C-MEX. The complexity is O(n + k.log(k)), where n is the size of the array, and k is the number of elements to be selected.
Faster than SORT or multiple call of MIN/MAX for large size inputs.
Multidimensional capability supported

Cite As

Bruno Luong (2024). Min/Max selection (https://www.mathworks.com/matlabcentral/fileexchange/23576-min-max-selection), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2006b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Find more on Matrix Indexing in Help Center and MATLAB Answers

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes

Disable inplace arrays for more recent MATLAB versions
Fix issue with dummy argument not assigned

Fix install issue with R2014

Fix C/C++ comment style issue

Change installation script, now copy prototype header file for V2010B or later.
Correct BUG when working on sparse matrix.

Specific installation for R2010B

Fix the bugs for sparse and all-NaN vector

Possibility to disable post-sorting step

Correct bug: cleanup the inplace variable when MEX issues an error (otherwise computer might crash)

Correct bug + inplace engine

handle arrays with NaN

Supported sparse input

Bug correction (for k=0)
Do not compute unnecessary location indexes when not required.