Find first k largest elements?
16 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Rubel Ahmed
am 13 Aug. 2021
Kommentiert: Matt J
am 13 Aug. 2021
Hi all
I have an array say A = [1 5 7 4 8 10 14]. I want to find most largest, second most largest and 3rd most largest elements from A.
I have figured out that it can be done using maxk function which is abailable for update version of Matlab but I am using R2017a where maxk function is not availablle.
Anyway, I want to see the output like Ans = [14 10 8]. Please help.Thanks in advance.
0 Kommentare
Akzeptierte Antwort
Matt J
am 13 Aug. 2021
Bearbeitet: Matt J
am 13 Aug. 2021
The efficient way would probably be to use a File Exchange MEX alternative to maxk() like this,
but you could also do,
A = [1 5 7 4 8 10 14];
B=sort(A,'descend');
B=B(1:3)
2 Kommentare
Matt J
am 13 Aug. 2021
I wouldn't recommend that.
A=rand(3e7,1);
tic;
B=maxk(A,3);
toc
tic;
B=sort(A,'descend');
B=B(1:3);
toc;
tic;
[~,idx]=sort(A);
B=A(idx(end:end-2));
toc
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Whos finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!