I need to generate the rank order of a vector like the
Excel RANK function. I.E.
Highest Number -> 1
Second Highest -> 2
etc.
I'm hoping that there's one in the library and I don't have
to write one.
If you'd just like the vector returned in ascending order,
try the sort() function. To find the position of a
particular number n in the sorted version of the original
vector v, try:
find(sort(v)==n)
This returns an array of indeces at which the elements of
the sorted vector are equal to n.
If you get stuck, press F1.
>>mat = [0.2 0.8 0.3 0.5 0.4];
>> [sorted, order] = sort(mat)
sorted =
0.2000 0.3000 0.4000 0.5000 0.8000
order =
1 3 5 4 2
the order matrix tells you that the smallest item is in the
first index of mat, the second-smallest is in the third
index of mat, etc. in your case, it will tell you that the
"highest rank" is in the very last position of the order
matrix, which tells you that index 2 in mat is the largest
item (0.8, which is true).
hope this helps.
NumberVector RankVector
10 1
9 2
8 3
8 3
6 4
2 5
Deals with ties etc.
Again Thanks in advance.
don
ranking=[1:5];
and then
mat_rank(order)=ranking;
I obtain an array that says the first element is first,
the second is fifth and so on.
This is good for me.
But I can't extend this result tu a matrix (i'm interested
in the relative position of each elements in the row)...
ex:
A=[0.1 0.2 -0.1 0; 0 -0.1 0.2 0.1];
ranking=[1:4];
[sorted,order]=sort(A,2)
obviously now the statement
A(order)=ranking doesn't
work.
But is there a similar way to make this (maybe using
repmat(ranking)) or have I to make a cycle on each row of my
dataset? (it's quite big, so I would like to vectorize this
function...)
Thank you very much in advance,
hope some of you has understood (I think I explained in a
really bad way what I need :p) and can help me.
Cheers -Luca-
one of the many solutions
v=10*[10,9,8,8,8,6,5,8].';
[r,r,r]=unique(v);
r=max(r)-r+1;
disp([v,r]);
us
The most important are:
Fractional Ranking (1 2.5 2.5 4)
Dense Ranking (1 2 2 3)
Standard Competition Ranking (1 2 2 4)
Modified Competition Ranking (1 3 3 4)
Ordinal Ranking (1 2 3 4) OR (1 3 2 4)
****************************
The beautiful solution provided by us is known as "Dense
Ranking".
But if you need to use the rankings for statistical
purposes, then the most important is probably the
Fractional Ranking because it is such that the sum of N
ranks is equal to sum([1:N]), so that the average rank of N
items is always the same.
****************************
I have written these functions and I have submitted them to
Matlab Central - File Exchange. They are still very
inefficient and inelegant, if you find bugs or if you find
better solutions I would greatly appreciate it. Thank you.
Francesco
[0 9 12 7; 3 0 8 2; 8 20 0 3; 25 2 18 0;] must be [1 3 4 2; 3 1 4 2; 3 4 1 2; 4 2 3 1;]. I am really interested if anyone could solve this problem.
Thank you in advance!
a=[0 9 12 7; 3 0 8 2; 8 20 0 3; 25 2 18 0]
[s i]=sort(a,2);
[J I]=ndgrid(1:size(i,1),1:size(i,2));
i(sub2ind(size(i),J,i))=I;
% Bruno
Jan
Thanks, James.
For *vector* "a", the second output of the command
[s i] = SORT(a)
satisfies (see doc)
s = a(i) where s is sorted, (eqt1).
Ranking 'r' is by definition a permutation such that
s(r) = a, (eqt2).
From (eqt1 & 2) we get:
s(r(i)) = a(i) = s = s(1:length(s))
Therefore by comparing the indexes of the most lhs and the most rhs of the equality we have:
r(i) = 1:length(s) (in other word, r and i are mutually inverse permutations)
Now if you apply the above for 'a' successively is the row vector of the array A(k,:) k=1,2,.... you 'll find the logic behind my code.
The confusing thing in the code is that I use the same array named 'i' (for 'r') in the latest statement to limit reuse the same memory. But I hope you get the logic of the code.
Bruno
function [ ranked ] = rank(vector)
%UNTITLED2 Summary of this function goes here
% Detailed explanation goes here
sorted_vector = sort(vector);
ranked = 1:length(vector);
for i=2:length(vector)
location = find(sorted_vector==sorted_vector(i));
if length(location)>1
for j=1:length(location)
ranked(location(j)) = sum(location)/length(location);
end
else
ranked(i) = i;
end
end
end
mail me if problems ama...@idsc.net.eg
-----------------------------------------------------------------------
function a = xlrank(X)
%
% function a = xlrank(X)
%
% Performs the same task as the Excel function RANK
%
% e.g. if X = [2 8 3]'
% then a = xlrank(X) returns [1 3 2]'
%
% also if X = [2 8 3; 3 2 8]'
% then a = xlrank(X) returns [1 3 2; 2 1 3]'
%
for j = 1:length(X(1,:))
[a1,order] = sort(X(:,j));
a(order,j) = 1:length(X(:,j));
end
end
-----------------------------------------------------------------------
Ioannis (John) Kingdom
[~,i]=sort(x);
[~,xRanks]=sort(i);
Cheers
Sebastian