Ranking vectors from highest to lowest including repeated ranks.
Afficher commentaires plus anciens
If I have a vector say [0.4 0.1 0.1 0.2 0.1 0.05 0.05], then I want to rank this vector with output [1 3 3 2 3 6 6] (i.e. ranking number in the usual way)
Currently I'm using
X = [0.4 0.1 0.1 0.2 0.1 0.05 0.05]
Rnk = floor(tiedrank(-X));
This outputs:
Rnk = [1 4 4 2 4 6 6]
But this doesn't work because I want the third ranked element to output 3 in each case, but tiedrank function is computing the mean(3,4,5) = 4. This method does work when there are only 2 repeats because, for example, for the 0.05 elements, they should be rank 6, but tiedrank function computes mean(6,7)=6.5 and floor(6.5) = 6, and my vectors usually have several entries that are similar:
For example let
X = [0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 ]
Rnk = floor(tiedrank(-X));
returns
Rnk = [5 5 5 5 5 5 5 5 5 5]
instead of [1 1 1 1 1 1 1 1 1 1 1] since the function computes mean(1,2,3,4,5,6,7,8,9,10) = 5.5 and floor(5.5) = 5. Is there a simple function that can yield me my desired output?
Réponse acceptée
Plus de réponses (1)
Hi William, I did it using a loop. Please let me know if the code below is what you wanted
x=[0.4 0.1 0.1 0.2 0.1 0.05 0.05];
out=zeros(size(x));
for j=1:length(x)
if max(x)>-Inf
sel=x==max(x);
out(sel)=sum(out>0)+1;
x(sel)=-Inf;
end
end
disp('Ranking')
disp(out)
Catégories
En savoir plus sur Descriptive Statistics dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!