MATLAB: Sort and assign ranking

sort

Hi I need to sort a vector and assign a ranking for the corresponding sorting order. I'm using sort function [sortedValue_X , X_Ranked] = sort(X,'descend'); but the problem is it assignes different ranks for the same values (zeros). ie x = [ 13 15 5 5 0 0 0 1 0 3] and i want zeros to take the same last rank which is 6 and fives needs to share the 3rd rank etc.. any suggestions?

Best Answer

You can use the third return argument of unique(): Try this:
X = [ 13 15 5 5 0 0 0 1 0 3]
[sortedValue_X , X_Ranked] = sort(X,'descend')
[uniqueValues, ia, ic]=unique(sortedValue_X)
X_Ranked = max(ic)-ic+1 % <- there's your answer!
In command window:
X_Ranked =
1 2 3 3 4 5 6 6 6 6