MATLAB: Sorting from the maximum value to minimum value in a vector

mathematicsmatricesmatrixmatrix arraysortvectorvectorization

Hi. I have a vector named A, e.g.
A=[2 5 8 7 8 9 3 7 6 5 4 1]
I want to sort the members of vector A from maximum to minimum values.
Also I want to find the indices of equal members in the sorted version of vector A.
How can I do that?
Thanks for your help.

Best Answer

A=[2 5 8 7 8 9 3 7 6 5 4 1];
Asorted = sort(A,'descend'); % sorted from max to min
[~,indx] = unique(Asorted);
equalpairs = [(setdiff(1:length(Asorted),indx)-1)' setdiff(1:length(Asorted),indx)']; % each row contains pair of indices with equal values