Elegant k-smallest argmin in Matlab

Is there a vacuum cleaner (like in, ideally built-in, I never used Matlab so I apologize if I missed something obvious) to make the k-smallest argmin in Matlab (ie if the array is [4,5,6 , 7], it should return the indices 1,2 in this order), except for such things as:

arr = [4,5,6,7]; [~, argmin1] = min(arr); arr(argmin1) = Inf; [~, argmin2] = min(arr); ... 
+5
source share
1 answer

Let's say we want the indices k smallest element in the arr array, then:

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

If you want k highest values, use descend instead of the argument.

+4
source

All Articles