Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
sorting, ranking

Subject: sorting, ranking

From: Wyatt

Date: 21 Aug, 2009 16:16:03

Message: 1 of 11

Hello All,

I have encountered some weird behavior with matlab when attempting to rank a list of numbers.

lets assume that I generate a vector of random numbers.

K>>A = rand(1,10)*100

A =

  Columns 1 through 8

   79.2207 95.9492 65.5741 3.5712 84.9129 93.3993 67.8735 75.7740

  Columns 9 through 10

   74.3132 39.2227


If I then sort these numbers

K>>[V,I] = sort(A)

V =

  Columns 1 through 8

    3.5712 39.2227 65.5741 67.8735 74.3132 75.7740 79.2207 84.9129

  Columns 9 through 10

   93.3993 95.9492


I =

     4 10 3 7 9 8 1 5 6 2


and wish to obtain their ranking

K>>temp = [1:10]

temp =

     1 2 3 4 5 6 7 8 9 10


K>> rank = temp(I)

rank =

     4 10 3 7 9 8 1 5 6 2


I should get back a permutation of the vector temp in terms of the ordered indices of sorted values from A, instead this code simply returns the I vector

the correct execution of the command rank = temp(I) should return

rank = [7 10 3 1 8 9 4 6 5 2]

Am I missing something here, or mistaken about how matlab should behave in this case? Or is there simply a bug?

thanks

Subject: sorting, ranking

From: Wyatt

Date: 21 Aug, 2009 16:28:02

Message: 2 of 11

I apologize for this post. I tested this out on another machine and couldn't reproduce the same problem.

Perhaps this is a problem with my platform. I'm running

7.6.0.324 (R2008a)

on a Mac Pro with

OS X 10.5.8

Subject: sorting, ranking

From: Wyatt

Date: 21 Aug, 2009 16:36:20

Message: 3 of 11

"Wyatt " <hello@hello.com> wrote in message <h6mhui$4pa$1@fred.mathworks.com>...
> I apologize for this post. I tested this out on another machine and couldn't reproduce the same problem.
>
> Perhaps this is a problem with my platform. I'm running
>
> 7.6.0.324 (R2008a)
>
> on a Mac Pro with
>
> OS X 10.5.8

actually, I stand corrected, after trying it once it works, but a second time it doesn't.

Subject: sorting, ranking

From: someone

Date: 21 Aug, 2009 16:50:22

Message: 4 of 11

"Wyatt " <hello@hello.com> wrote in message <h6mh83$j42$1@fred.mathworks.com>...
> Hello All,
>
> I have encountered some weird behavior with matlab when attempting to rank a list of numbers.
>
> lets assume that I generate a vector of random numbers.
>
> K>>A = rand(1,10)*100
>
> A =
>
> Columns 1 through 8
>
> 79.2207 95.9492 65.5741 3.5712 84.9129 93.3993 67.8735 75.7740
>
> Columns 9 through 10
>
> 74.3132 39.2227
>
>
> If I then sort these numbers
>
> K>>[V,I] = sort(A)
>
> V =
>
> Columns 1 through 8
>
> 3.5712 39.2227 65.5741 67.8735 74.3132 75.7740 79.2207 84.9129
>
> Columns 9 through 10
>
> 93.3993 95.9492
>
>
> I =
>
> 4 10 3 7 9 8 1 5 6 2
>
>
> and wish to obtain their ranking
>
> K>>temp = [1:10]
>
> temp =
>
> 1 2 3 4 5 6 7 8 9 10
>
>
> K>> rank = temp(I)
>
> rank =
>
> 4 10 3 7 9 8 1 5 6 2
>
>
> I should get back a permutation of the vector temp in terms of the ordered indices of sorted values from A, instead this code simply returns the I vector
>
> the correct execution of the command rank = temp(I) should return
>
> rank = [7 10 3 1 8 9 4 6 5 2]
>
> Am I missing something here, or mistaken about how matlab should behave in this case? Or is there simply a bug?
>
> thanks

Maybe I'm missing something here.
Why would you expect the above code NOT to return the I vector?
(I get the same result as you do above.)

Subject: sorting, ranking

From: Steven Lord

Date: 21 Aug, 2009 16:56:04

Message: 5 of 11


"Wyatt " <hello@hello.com> wrote in message
news:h6mh83$j42$1@fred.mathworks.com...
> Hello All,
>
> I have encountered some weird behavior with matlab when attempting to rank
> a list of numbers.
>
> lets assume that I generate a vector of random numbers.

*snip*

> If I then sort these numbers

*snip*

> and wish to obtain their ranking

*snip*

> I should get back a permutation of the vector temp in terms of the ordered
> indices of sorted values from A, instead this code simply returns the I
> vector
>
> the correct execution of the command rank = temp(I) should return
>
> rank = [7 10 3 1 8 9 4 6 5 2]
>
> Am I missing something here, or mistaken about how matlab should behave in
> this case? Or is there simply a bug?

You have it backwards. You want "rank(I) = temp" instead.

S = 100*rand(1, 10)
[value, order] = sort(S)
restoreS(order) = value
isequal(restoreS, S)

From the reference page:

http://www.mathworks.com/access/helpdesk/help/techdoc/ref/sort.html

"[B,IX] = sort(A,...) also returns an array of indices IX, where size(IX) ==
size(A). If A is a vector, B = A(IX)." [or exchanging the two sides of the
equation, A(IX) = B.]

So the 1st element of B, the sorted array, corresponds to element IX(1) in
A -- and that's the assignment you need to make to "undo" the sorting. To
generalize, B(k) corresponds to A(IX(k)), or if you vectorize newA(IX) = B
undoes the sort and makes A and newA agree.

--
Steve Lord
slord@mathworks.com

Subject: sorting, ranking

From: Wyatt

Date: 21 Aug, 2009 18:25:18

Message: 6 of 11

that did the trick steve. It just seems to me that

rank = temp(I)

should be valid syntax, and is more intuitive than writing

rank(I) = temp

perhaps because I'm thinking that if I wanted to arbitrarily pull out values [7 4 9 1] from a vector A I would write

A([7 4 9 1])

and it would give me the 7th 4th 9th and 1st values from A in that order.

Subject: sorting, ranking

From: someone

Date: 21 Aug, 2009 18:53:20

Message: 7 of 11

"Wyatt " <hello@hello.com> wrote in message <h6moqe$sqf$1@fred.mathworks.com>...
> that did the trick steve. It just seems to me that
>
> rank = temp(I)
>
> should be valid syntax, and is more intuitive than writing
>
> rank(I) = temp
>
> perhaps because I'm thinking that if I wanted to arbitrarily pull out values [7 4 9 1] from a vector A I would write
>
> A([7 4 9 1])
>
> and it would give me the 7th 4th 9th and 1st values from A in that order.

But to me, this would not be very intuitive at all.

since A([9]) is the same as A(9),
A([9]) should return the 9th index of A (as it does).

Subject: sorting, ranking

From: Steven Lord

Date: 21 Aug, 2009 19:53:53

Message: 8 of 11


"Wyatt " <hello@hello.com> wrote in message
news:h6moqe$sqf$1@fred.mathworks.com...
> that did the trick steve. It just seems to me that
>
> rank = temp(I)
>
> should be valid syntax,

It is. It just doesn't do what you expect it to do.

> and is more intuitive than writing
>
> rank(I) = temp

Why do you say that? "rank = temp(I)" is a valid MATLAB command in this
scenario, but so is "rank = I.^2" or "rank = sin(cos(tan(I)))". Each of
those commands do something different; for the application you described in
your original post, the command that does what you want is "rank(I) = temp".

Although you probably shouldn't use rank as a variable name, unless you're
okay with not being able to use the RANK function in the same piece of code.

> perhaps because I'm thinking that if I wanted to arbitrarily pull out
> values [7 4 9 1] from a vector A I would write
>
> A([7 4 9 1])
>
> and it would give me the 7th 4th 9th and 1st values from A in that order.

Yes. And if you wanted to store 1:4 into the 7th, 4th, 9th, and 1st
elements of A, you'd use:

A([7 4 9 1]) = (1:4);

Just because one command works does not necessarily rule out a modification
of that command also working and doing something slightly different.

--
Steve Lord
slord@mathworks.com

Subject: sorting, ranking

From: Nir

Date: 14 Jan, 2013 16:33:21

Message: 9 of 11

Dear all,

Here is a one-liner for getting ranks with ties:
[~,rank_vector]=ismember(data_vector,unique(sort(data_vector)));


Nir



"Steven Lord" <slord@mathworks.com> wrote in message <h6mu08$aua$1@fred.mathworks.com>...
>
> "Wyatt " <hello@hello.com> wrote in message
> news:h6moqe$sqf$1@fred.mathworks.com...
> > that did the trick steve. It just seems to me that
> >
> > rank = temp(I)
> >
> > should be valid syntax,
>
> It is. It just doesn't do what you expect it to do.
>
> > and is more intuitive than writing
> >
> > rank(I) = temp
>
> Why do you say that? "rank = temp(I)" is a valid MATLAB command in this
> scenario, but so is "rank = I.^2" or "rank = sin(cos(tan(I)))". Each of
> those commands do something different; for the application you described in
> your original post, the command that does what you want is "rank(I) = temp".
>
> Although you probably shouldn't use rank as a variable name, unless you're
> okay with not being able to use the RANK function in the same piece of code.
>
> > perhaps because I'm thinking that if I wanted to arbitrarily pull out
> > values [7 4 9 1] from a vector A I would write
> >
> > A([7 4 9 1])
> >
> > and it would give me the 7th 4th 9th and 1st values from A in that order.
>
> Yes. And if you wanted to store 1:4 into the 7th, 4th, 9th, and 1st
> elements of A, you'd use:
>
> A([7 4 9 1]) = (1:4);
>
> Just because one command works does not necessarily rule out a modification
> of that command also working and doing something slightly different.
>
> --
> Steve Lord
> slord@mathworks.com
>

Subject: sorting, ranking

From: Bruno Luong

Date: 14 Jan, 2013 18:21:34

Message: 10 of 11

"Nir" wrote in message <kd1c0h$1pl$1@newscl01ah.mathworks.com>...
> Dear all,
>
> Here is a one-liner for getting ranks with ties:
> [~,rank_vector]=ismember(data_vector,unique(sort(data_vector)));

No need to cascade sort and unique:

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

Bruno

Subject: sorting, ranking

From: Bruno Luong

Date: 14 Jan, 2013 18:25:28

Message: 11 of 11

"Nir" wrote in message <kd1c0h$1pl$1@newscl01ah.mathworks.com>...
> Dear all,
>
> Here is a one-liner for getting ranks with ties:
> [~,rank_vector]=ismember(data_vector,unique(sort(data_vector)));
>


Or simply:
[~,~,rank_vector] = unique(data_vector)

% Bruno

Tags for this Thread

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.

Contact us