MATLAB: Return Indices for x Smallest/Largest Values in Array

maxmin

Is there a clean and elegant way for extracting the indices of the x smallest or largest values in an array? For example say I have an array [1,6,4,9,0] and I want the indices of the 3 smallest values, [1,3,5]. The functions min/max only return the index for the single minimum/maximum.

Best Answer

Use the second output of the mink or maxk functions.