Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab rank values in vector with elements repeated multiple times

Tags:

sorting

matlab

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?

like image 474
MeSS83 Avatar asked Nov 11 '15 11:11

MeSS83


2 Answers

unique with 'stable' makes this easy:

[srt, idxSrt]  = sort(dataVector,'descend');
[~,rnk,idxrnk] = unique(srt, 'stable');
unsorted = rnk(idxrnk);

result = unsorted(idxSrt)'
like image 66
Dan Avatar answered Oct 03 '22 16:10

Dan


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
like image 45
Luis Mendo Avatar answered Oct 03 '22 14:10

Luis Mendo