MATLAB: Extract N maximum values from a vector, with also the relative index position

vectors

Hi, I have a vector and I want to take for example the top 5 maximum values presented in it, but I need also the index position where these 5 values are placed. How I can do? Thank you.

Best Answer

sort your vector in descending order. The first 5 values are the top 5 maximum (they may be identical) and the first 5 elements of the second return value of sort is their index:
v = rand(1, 100);
[sortedv, indices] = sort(v, 'descend');
max5 = sortedv(1:5)
idx5 = indices(1:5)