Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

elegant k-smallest argmin in Matlab

Tags:

Is there a cleaner (as in, ideally built in; I've never used Matlab so apologies if I missed something obvious) way to do k-smallest argmin in Matlab (i.e. if an array is [4,5,6,7] it should return the indexes 1,2 in that order) besides stuff like:

arr = [4,5,6,7];
[~, argmin1] = min(arr);
arr(argmin1) = Inf;
[~, argmin2] = min(arr);
...
like image 522
houbysoft Avatar asked May 02 '16 07:05

houbysoft


1 Answers

Say we want indices of k smallest element in array arr, then:

arr=[4,5,6,7,2];
[~,indices]=sort(arr,'ascend');
argmin=indices(1:k);

If one want k highest values, use descend argument instead.

like image 138
Crowley Avatar answered Sep 28 '22 04:09

Crowley