Re: Ordering function weird?
- To: mathgroup at smc.vnet.net
- Subject: [mg82693] Re: Ordering function weird?
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Sun, 28 Oct 2007 04:10:00 -0500 (EST)
- Organization: The Open University, Milton Keynes, UK
- References: <ffv29v$aph$1@smc.vnet.net>
Claus wrote: > say I've got two sets of number, x and y, which I want to rank. See the > example below. I totally expect and want the result of Ordering[x]. But > I neiter understand nor expect the result of Ordering[y]. Both Sort[x] > and Sort[{1, 2, 7, 8, 9, 1, 2}] are ok. From the online help, "Ordering[list] gives the *positions* in list at which each successive element of Sort[list] appears." (Emphasize added.) In other words, *Ordering* returns a list of _indices_ rather than a list of sorted elements. For instance, Ordering[{1, 2, 7, 8, 9, 1, 2}] returns the list of indices {1, 6, 2, 7, 3, 4, 5} that must be read as, "If one wants the list of integers {1, 2, 7, 8, 9, 1, 2} in ascending order (the default), one must take the first integer of the original list (i.e. one), then the sixth integer (i.e. one again but at a different position), then the second (i.e. two), then the seventh (i.e. two again but also at a different position), then the third (i.e. seven), then the fourth (i.e. eight, and finally the fifth (i.e. nine) resulting in the new sorted in ascending order list {1, 1, 2, 2, 7, 8, 9}, list which is identical to what is returned by Sort[{1, 2, 7, 8, 9, 1, 2}]. In[1]:= x = {1, 2, 3, 6, 10, 3, 4}; y = {1, 2, 7, 8, 9, 1, 2}; Sort[x] Sort[y] Out[3]= {1, 2, 3, 3, 4, 6, 10} Out[4]= {1, 1, 2, 2, 7, 8, 9} In[5]:= Ordering[x] Ordering[y] Out[5]= {1, 2, 3, 6, 7, 4, 5} Out[6]= {1, 6, 2, 7, 3, 4, 5} In[7]:= x[[%%]] y[[%%]] Out[7]= {1, 2, 3, 3, 4, 6, 10} Out[8]= {1, 1, 2, 2, 7, 8, 9} In[9]:= {x[[Ordering[x]]] === Sort[x], y[[Ordering[y]]] === Sort[y]} Out[9]= {True, True} Regards, -- Jean-Marc