File Exchange

image thumbnail

A C-mex Function to find N largest number in a Matrix

version 1.0.0.0 (6.22 KB) by Changshun Deng
A c-mex function to find N largest number in a matrix.

1 Download

Updated 28 Sep 2005

No License

This is a C-Mex Fucntion to find n largest number in a matrix.

>> x=randperm(20);
>> nmax(x,5)

ans =

20 19 18 17 16

>> nthmax(x,5)

ans =

16

Cite As

Changshun Deng (2020). A C-mex Function to find N largest number in a Matrix (https://www.mathworks.com/matlabcentral/fileexchange/8568-a-c-mex-function-to-find-n-largest-number-in-a-matrix), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (6)

Giovani Tonel

bainhome webID

support my friend£¡

Rudran Rudran

Changshun Deng

>> x=rand(1000,10000);

>> tic;y = sort(x) ; y(end-10);toc

elapsed_time =

17.8590

>> tic;nmax(x,10);toc

elapsed_time =

3.7350

Changshun Deng

i don't know if run my program.
just compare these results:

>> tic;nmax(x,1);toc

elapsed_time =

0.2030

>> tic;max(x);toc

elapsed_time =

1.3910

>> x=rand(1000,10000);
>> tic;nmax(x,10);toc

elapsed_time =

10.1880

>> tic;y = sort(x) ; y(end-10);toc

elapsed_time =

29.4380

Jos x

Same as
b = sort(a) ; b(end-N)
or
b = unique(a) ; b(end-N)
depending on the exact definition of Nth largest number. Both sort and unique are faster than your routines ...

MATLAB Release Compatibility
Created with R13SP1
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!