<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/259068</link>
    <title>MATLAB Central Newsreader - sorting, ranking</title>
    <description>Feed for thread: sorting, ranking</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Fri, 21 Aug 2009 16:16:03 -0400</pubDate>
      <title>sorting, ranking</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/259068#674885</link>
      <author>Wyatt </author>
      <description>Hello All,&lt;br&gt;
&lt;br&gt;
I have encountered some weird behavior with matlab when attempting to rank a list of numbers.&lt;br&gt;
&lt;br&gt;
lets assume that I generate a vector of random numbers.&lt;br&gt;
&lt;br&gt;
K&amp;gt;&amp;gt;A = rand(1,10)*100&lt;br&gt;
&lt;br&gt;
A =&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;Columns 1 through 8&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;79.2207   95.9492   65.5741    3.5712   84.9129   93.3993   67.8735   75.7740&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;Columns 9 through 10&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;74.3132   39.2227&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
If I then sort these numbers&lt;br&gt;
&lt;br&gt;
K&amp;gt;&amp;gt;[V,I] = sort(A)&lt;br&gt;
&lt;br&gt;
V =&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;Columns 1 through 8&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;3.5712   39.2227   65.5741   67.8735   74.3132   75.7740   79.2207   84.9129&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;Columns 9 through 10&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;93.3993   95.9492&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
I =&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;4    10     3     7     9     8     1     5     6     2&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
and wish to obtain their ranking&lt;br&gt;
&lt;br&gt;
K&amp;gt;&amp;gt;temp = [1:10]&lt;br&gt;
&lt;br&gt;
temp =&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1     2     3     4     5     6     7     8     9    10&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
K&amp;gt;&amp;gt; rank = temp(I)&lt;br&gt;
&lt;br&gt;
rank =&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;4    10     3     7     9     8     1     5     6     2&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
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&lt;br&gt;
&lt;br&gt;
the correct execution of the command rank = temp(I) should return &lt;br&gt;
&lt;br&gt;
rank = [7 10 3 1 8 9 4 6 5 2]&lt;br&gt;
&lt;br&gt;
Am I missing something here, or mistaken about how matlab should behave in this case?  Or is there simply a bug?&lt;br&gt;
&lt;br&gt;
thanks</description>
    </item>
    <item>
      <pubDate>Fri, 21 Aug 2009 16:28:02 -0400</pubDate>
      <title>Re: sorting, ranking</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/259068#674889</link>
      <author>Wyatt </author>
      <description>I apologize for this post.  I tested this out on another machine and couldn't reproduce the same problem.  &lt;br&gt;
&lt;br&gt;
Perhaps this is a problem with my platform.  I'm running&lt;br&gt;
&lt;br&gt;
7.6.0.324 (R2008a)&lt;br&gt;
&lt;br&gt;
on a Mac Pro with&lt;br&gt;
&lt;br&gt;
OS  X 10.5.8 </description>
    </item>
    <item>
      <pubDate>Fri, 21 Aug 2009 16:36:20 -0400</pubDate>
      <title>Re: sorting, ranking</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/259068#674892</link>
      <author>Wyatt </author>
      <description>&quot;Wyatt &quot; &amp;lt;hello@hello.com&amp;gt; wrote in message &amp;lt;h6mhui$4pa$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; I apologize for this post.  I tested this out on another machine and couldn't reproduce the same problem.  &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Perhaps this is a problem with my platform.  I'm running&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; 7.6.0.324 (R2008a)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; on a Mac Pro with&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; OS  X 10.5.8 &lt;br&gt;
&lt;br&gt;
actually, I stand corrected, after trying it once it works, but a second time it doesn't.</description>
    </item>
    <item>
      <pubDate>Fri, 21 Aug 2009 16:50:22 -0400</pubDate>
      <title>Re: sorting, ranking</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/259068#674896</link>
      <author>someone </author>
      <description>&quot;Wyatt &quot; &amp;lt;hello@hello.com&amp;gt; wrote in message &amp;lt;h6mh83$j42$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Hello All,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I have encountered some weird behavior with matlab when attempting to rank a list of numbers.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; lets assume that I generate a vector of random numbers.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; K&amp;gt;&amp;gt;A = rand(1,10)*100&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; A =&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;   Columns 1 through 8&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;    79.2207   95.9492   65.5741    3.5712   84.9129   93.3993   67.8735   75.7740&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;   Columns 9 through 10&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;    74.3132   39.2227&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; If I then sort these numbers&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; K&amp;gt;&amp;gt;[V,I] = sort(A)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; V =&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;   Columns 1 through 8&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;     3.5712   39.2227   65.5741   67.8735   74.3132   75.7740   79.2207   84.9129&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;   Columns 9 through 10&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;    93.3993   95.9492&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I =&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;      4    10     3     7     9     8     1     5     6     2&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; and wish to obtain their ranking&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; K&amp;gt;&amp;gt;temp = [1:10]&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; temp =&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;      1     2     3     4     5     6     7     8     9    10&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; K&amp;gt;&amp;gt; rank = temp(I)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; rank =&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;      4    10     3     7     9     8     1     5     6     2&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; 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&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; the correct execution of the command rank = temp(I) should return &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; rank = [7 10 3 1 8 9 4 6 5 2]&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Am I missing something here, or mistaken about how matlab should behave in this case?  Or is there simply a bug?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; thanks&lt;br&gt;
&lt;br&gt;
Maybe I'm missing something here.&lt;br&gt;
Why would you expect the above code NOT to return the I vector?&lt;br&gt;
(I get the same result as you do above.)</description>
    </item>
    <item>
      <pubDate>Fri, 21 Aug 2009 16:56:04 -0400</pubDate>
      <title>Re: sorting, ranking</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/259068#674898</link>
      <author>Steven Lord</author>
      <description>&lt;br&gt;
&quot;Wyatt &quot; &amp;lt;hello@hello.com&amp;gt; wrote in message &lt;br&gt;
news:h6mh83$j42$1@fred.mathworks.com...&lt;br&gt;
&amp;gt; Hello All,&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; I have encountered some weird behavior with matlab when attempting to rank &lt;br&gt;
&amp;gt; a list of numbers.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; lets assume that I generate a vector of random numbers.&lt;br&gt;
&lt;br&gt;
*snip*&lt;br&gt;
&lt;br&gt;
&amp;gt; If I then sort these numbers&lt;br&gt;
&lt;br&gt;
*snip*&lt;br&gt;
&lt;br&gt;
&amp;gt; and wish to obtain their ranking&lt;br&gt;
&lt;br&gt;
*snip*&lt;br&gt;
&lt;br&gt;
&amp;gt; I should get back a permutation of the vector temp in terms of the ordered &lt;br&gt;
&amp;gt; indices of sorted values from A, instead this code simply returns the I &lt;br&gt;
&amp;gt; vector&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; the correct execution of the command rank = temp(I) should return&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; rank = [7 10 3 1 8 9 4 6 5 2]&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Am I missing something here, or mistaken about how matlab should behave in &lt;br&gt;
&amp;gt; this case?  Or is there simply a bug?&lt;br&gt;
&lt;br&gt;
You have it backwards.  You want &quot;rank(I) = temp&quot; instead.&lt;br&gt;
&lt;br&gt;
S = 100*rand(1, 10)&lt;br&gt;
[value, order] = sort(S)&lt;br&gt;
restoreS(order) = value&lt;br&gt;
isequal(restoreS, S)&lt;br&gt;
&lt;br&gt;
From the reference page:&lt;br&gt;
&lt;br&gt;
&lt;a href=&quot;http://www.mathworks.com/access/helpdesk/help/techdoc/ref/sort.html&quot;&gt;http://www.mathworks.com/access/helpdesk/help/techdoc/ref/sort.html&lt;/a&gt;&lt;br&gt;
&lt;br&gt;
&quot;[B,IX] = sort(A,...) also returns an array of indices IX, where size(IX) == &lt;br&gt;
size(A). If A is a vector, B = A(IX).&quot; [or exchanging the two sides of the &lt;br&gt;
equation, A(IX) = B.]&lt;br&gt;
&lt;br&gt;
So the 1st element of B, the sorted array, corresponds to element IX(1) in &lt;br&gt;
A -- and that's the assignment you need to make to &quot;undo&quot; the sorting.  To &lt;br&gt;
generalize, B(k) corresponds to A(IX(k)), or if you vectorize newA(IX) = B &lt;br&gt;
undoes the sort and makes A and newA agree.&lt;br&gt;
&lt;br&gt;
-- &lt;br&gt;
Steve Lord&lt;br&gt;
slord@mathworks.com </description>
    </item>
    <item>
      <pubDate>Fri, 21 Aug 2009 18:25:18 -0400</pubDate>
      <title>Re: sorting, ranking</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/259068#674921</link>
      <author>Wyatt </author>
      <description>that did the trick steve.  It just seems to me that &lt;br&gt;
&lt;br&gt;
rank = temp(I)&lt;br&gt;
&lt;br&gt;
should be valid syntax, and is more intuitive than writing &lt;br&gt;
&lt;br&gt;
rank(I) = temp&lt;br&gt;
&lt;br&gt;
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&lt;br&gt;
&lt;br&gt;
A([7 4 9 1])&lt;br&gt;
&lt;br&gt;
and it would give me the 7th 4th 9th and 1st values from A in that order.</description>
    </item>
    <item>
      <pubDate>Fri, 21 Aug 2009 18:53:20 -0400</pubDate>
      <title>Re: sorting, ranking</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/259068#674925</link>
      <author>someone </author>
      <description>&quot;Wyatt &quot; &amp;lt;hello@hello.com&amp;gt; wrote in message &amp;lt;h6moqe$sqf$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; that did the trick steve.  It just seems to me that &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; rank = temp(I)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; should be valid syntax, and is more intuitive than writing &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; rank(I) = temp&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; 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&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; A([7 4 9 1])&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; and it would give me the 7th 4th 9th and 1st values from A in that order.&lt;br&gt;
&lt;br&gt;
But to me, this would not be very intuitive at all.&lt;br&gt;
&lt;br&gt;
since A([9]) is the same as A(9),&lt;br&gt;
A([9]) should return the 9th index of A (as it does).</description>
    </item>
    <item>
      <pubDate>Fri, 21 Aug 2009 19:53:53 -0400</pubDate>
      <title>Re: sorting, ranking</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/259068#674933</link>
      <author>Steven Lord</author>
      <description>&lt;br&gt;
&quot;Wyatt &quot; &amp;lt;hello@hello.com&amp;gt; wrote in message &lt;br&gt;
news:h6moqe$sqf$1@fred.mathworks.com...&lt;br&gt;
&amp;gt; that did the trick steve.  It just seems to me that&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; rank = temp(I)&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; should be valid syntax,&lt;br&gt;
&lt;br&gt;
It is.  It just doesn't do what you expect it to do.&lt;br&gt;
&lt;br&gt;
&amp;gt; and is more intuitive than writing&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; rank(I) = temp&lt;br&gt;
&lt;br&gt;
Why do you say that?  &quot;rank = temp(I)&quot; is a valid MATLAB command in this &lt;br&gt;
scenario, but so is &quot;rank = I.^2&quot; or &quot;rank = sin(cos(tan(I)))&quot;.  Each of &lt;br&gt;
those commands do something different; for the application you described in &lt;br&gt;
your original post, the command that does what you want is &quot;rank(I) = temp&quot;.&lt;br&gt;
&lt;br&gt;
Although you probably shouldn't use rank as a variable name, unless you're &lt;br&gt;
okay with not being able to use the RANK function in the same piece of code.&lt;br&gt;
&lt;br&gt;
&amp;gt; perhaps because I'm thinking that if I wanted to arbitrarily pull out &lt;br&gt;
&amp;gt; values [7 4 9 1] from a vector A I would write&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; A([7 4 9 1])&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; and it would give me the 7th 4th 9th and 1st values from A in that order.&lt;br&gt;
&lt;br&gt;
Yes.  And if you wanted to store 1:4 into the 7th, 4th, 9th, and 1st &lt;br&gt;
elements of A, you'd use:&lt;br&gt;
&lt;br&gt;
A([7 4 9 1]) = (1:4);&lt;br&gt;
&lt;br&gt;
Just because one command works does not necessarily rule out a modification &lt;br&gt;
of that command also working and doing something slightly different.&lt;br&gt;
&lt;br&gt;
-- &lt;br&gt;
Steve Lord&lt;br&gt;
slord@mathworks.com </description>
    </item>
  </channel>
</rss>

