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);
...
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With