Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to? Excel RANK function in Matlab?

913 views
Skip to first unread message

Don

unread,
Feb 1, 2008, 10:25:04 AM2/1/08
to
Thanks in advance

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.


Willem Geert

unread,
Feb 1, 2008, 10:37:01 AM2/1/08
to
"Don " <ya...@lsua.edu> wrote in message
<fnvdkf$set$1...@fred.mathworks.com>...


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.

Ian Clarkson

unread,
Feb 1, 2008, 10:46:25 AM2/1/08
to
"Don " <ya...@lsua.edu> wrote in message
<fnvdkf$set$1...@fred.mathworks.com>...

>>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.

Don

unread,
Feb 1, 2008, 10:52:01 AM2/1/08
to
"Willem Geert " <wgp...@gmail.com> wrote in message
<fnveat$9j0$1...@fred.mathworks.com>...
Thanks, that works well but what I wanted was a little
different. Example

NumberVector RankVector
10 1
9 2
8 3
8 3
6 4
2 5

Deals with ties etc.

Again Thanks in advance.
don

Luca Cerone

unread,
Feb 12, 2008, 6:06:02 PM2/12/08
to
Hi, I have to do almost the same as you said, but
with matrix:
in the example you wrote, to know the ranking for a
vector it's sufficient to create
something like

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-

us

unread,
Feb 12, 2008, 6:22:02 PM2/12/08
to
"Don ":
<SNIP wants to wear a tie...

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

Francesco Pozzi

unread,
Apr 8, 2008, 8:18:03 PM4/8/08
to
There exist many ranking functions, check here:
http://en.wikipedia.org/wiki/Ranking.

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

Jan Sonneveld

unread,
Nov 2, 2008, 7:30:04 AM11/2/08
to
After reading this topic I can't solve my 'challenge'. I want to rank a matrix, but also the entries of the 'rank matrix' has to be at the same places as in the normal matrix. For instance:

[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!

Bruno Luong

unread,
Nov 2, 2008, 8:36:02 AM11/2/08
to
"Jan Sonneveld" <j.son...@student.tudelft.nl> wrote in message <gek6gc$ncn$1...@fred.mathworks.com>...

> After reading this topic I can't solve my 'challenge'. I want to rank a matrix, but also the entries of the 'rank matrix' has to be at the same places as in the normal matrix. For instance:
>
> [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.
>

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 Sonneveld

unread,
Nov 3, 2008, 5:54:01 PM11/3/08
to
"Bruno Luong" <b.l...@fogale.findmycountry> wrote in message <gekac2$r3h$1...@fred.mathworks.com>...
Hi bruno!
This solves my problem! Thanx

Jan

james bejon

unread,
Mar 31, 2010, 5:46:04 AM3/31/10
to
I know this is a really old thread, but could someone please explain to me how Bruno's solution works? I can't see how it arrives at the correct answer.

Thanks, James.

Bruno Luong

unread,
Mar 31, 2010, 7:13:04 AM3/31/10
to
"james bejon" <james...@yahoo.co.uk> wrote in message <hov5ks$2lj$1...@fred.mathworks.com>...

> I know this is a really old thread, but could someone please explain to me how Bruno's solution works? I can't see how it arrives at the correct answer.
>
> 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

james bejon

unread,
Mar 31, 2010, 2:18:20 PM3/31/10
to
I've read (and stepped) through this code a few times now. And I think I get it. Very nice move.

IDSC Badr

unread,
May 10, 2010, 2:57:04 AM5/10/10
to
"Don " <ya...@lsua.edu> wrote in message <fnvdkf$set$1...@fred.mathworks.com>...
Dear don
Please try this piece of code

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

John Kingdom

unread,
Jun 6, 2011, 11:23:20 AM6/6/11
to
"Don " <ya...@lsua.edu> wrote in message <fnvdkf$set$1...@fred.mathworks.com>...
A function I've just written to do the job:

-----------------------------------------------------------------------
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

Sebastian

unread,
Aug 11, 2011, 4:43:10 AM8/11/11
to
How about

[~,i]=sort(x);
[~,xRanks]=sort(i);

Cheers
Sebastian

Claudia

unread,
Jun 10, 2012, 9:45:08 PM6/10/12
to
Hi Sebastian

Just had the same problem and was looking for a solution.
I prefer your one! so simple (after knowing it :-) ) and so good !!! I like it that way!

Cheers, Claudia



"Sebastian" wrote in message <j204mu$7k4$1...@newscl01ah.mathworks.com>...

Nir

unread,
Jan 14, 2013, 11:34:07 AM1/14/13
to
Dear all,

Here is a one-liner for getting ranks with toes:

[~,rank_vector]=ismember(data_vector,unique(sort(data_vector)));


Nir

"Claudia " <claudia...@gmx.de> wrote in message <jr3ij4$el6$1...@newscl01ah.mathworks.com>...
0 new messages