Thread Subject: How to? Excel RANK function in Matlab?

Subject: How to? Excel RANK function in Matlab?

From: Don

Date: 1 Feb, 2008 15:25:04

Message: 1 of 10

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.


Subject: How to? Excel RANK function in Matlab?

From: Willem Geert

Date: 1 Feb, 2008 15:37:01

Message: 2 of 10

"Don " <yates@lsua.edu> wrote in message
<fnvdkf$set$1@fred.mathworks.com>...
> 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.
>
>


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.

Subject: How to? Excel RANK function in Matlab?

From: Ian Clarkson

Date: 1 Feb, 2008 15:46:25

Message: 3 of 10

"Don " <yates@lsua.edu> wrote in message
<fnvdkf$set$1@fred.mathworks.com>...
> 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.
>
>

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

Subject: How to? Excel RANK function in Matlab?

From: Don

Date: 1 Feb, 2008 15:52:01

Message: 4 of 10

"Willem Geert " <wgphaff@gmail.com> wrote in message
<fnveat$9j0$1@fred.mathworks.com>...
> "Don " <yates@lsua.edu> wrote in message
> <fnvdkf$set$1@fred.mathworks.com>...
> > 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.
> >
> >
>
>
> 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.
>
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

Subject: How to? Excel RANK function in Matlab?

From: Luca Cerone

Date: 12 Feb, 2008 23:06:02

Message: 5 of 10

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

Subject: How to? Excel RANK function in Matlab?

From: us

Date: 12 Feb, 2008 23:22:02

Message: 6 of 10

"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

Subject: How to? Excel RANK function in Matlab?

From: Francesco Pozzi

Date: 9 Apr, 2008 00:18:03

Message: 7 of 10

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

Subject: How to? Excel RANK function in Matlab?

From: Jan Sonneveld

Date: 2 Nov, 2008 12:30:04

Message: 8 of 10

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!

Subject: How to? Excel RANK function in Matlab?

From: Bruno Luong

Date: 2 Nov, 2008 13:36:02

Message: 9 of 10

"Jan Sonneveld" <j.sonneveld@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

Subject: How to? Excel RANK function in Matlab?

From: Jan Sonneveld

Date: 3 Nov, 2008 22:54:01

Message: 10 of 10

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <gekac2$r3h$1@fred.mathworks.com>...
> "Jan Sonneveld" <j.sonneveld@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
Hi bruno!
This solves my problem! Thanx

Jan

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
standard competition ranking Francesco Pozzi 8 Apr, 2008 20:20:11
dense ranking Francesco Pozzi 8 Apr, 2008 20:20:11
modified competition ranking Francesco Pozzi 8 Apr, 2008 20:20:11
fractional ranking Francesco Pozzi 8 Apr, 2008 20:20:11
ordinal ranking Francesco Pozzi 8 Apr, 2008 20:20:11
unique us 12 Feb, 2008 18:24:56
code us 12 Feb, 2008 18:24:56
rssFeed for this Thread

Public Submission Policy

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.

Contact us at files@mathworks.com