I would like to rank (order) the elements of a vector in Matlab and that elements with the same value have the same rank (in descending order). So I would like a routine such as:
>> Rank = ComputeRanking([ 5 10 5 5 1])
Rank =
2 1 2 2 5
I found a partial solution on the mathworks site: ranking values:
function vecRank = ComputeRanking2(dataVector)
%
% Sort data in descending order with duplicates
%
[srt, idxSrt] = sort(dataVector,'descend');
% Find where are the repetitions
idxRepeat = [false; diff(srt) == 0];
% Rank with tieds but w/o skipping
rnkNoSkip = cumsum(~idxRepeat);
% Preallocate rank
vecRank = 1:numel(dataVector);
% Adjust for tieds (and skip)
vecRank (idxRepeat) = rnkNoSkip(idxRepeat);
% Sort back
vecRank (idxSrt) = vecRank ;
end
This works if there is one duplicate ( 2 elements with the same value) but if there are 2 or more, as in my example, it does not work. How can I do to handle an arbitrary number of duplicates?
unique
with 'stable'
makes this easy:
[srt, idxSrt] = sort(dataVector,'descend');
[~,rnk,idxrnk] = unique(srt, 'stable');
unsorted = rnk(idxrnk);
result = unsorted(idxSrt)'
Here's another way: the rank of each element is 1 plus the number of unique elements that are smaller than that element:
result = 1 + sum(bsxfun(@lt, dataVector(:).', dataVector(:)), 1);
For dataVector = [5 10 5 5 1];
this gives
result =
2 1 2 2 5
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With