MathGroup Archive 2000

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: how to rank a list of elements?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg23140] Re: how to rank a list of elements?
  • From: "Allan Hayes" <hay at haystack.demon.co.uk>
  • Date: Thu, 20 Apr 2000 03:20:59 -0400 (EDT)
  • References: <8djk08$7dc@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Wen-Feng:

One possibility.

data = {15, 7, 15, 20, 1};

Replace[data, Flatten[Apply[Rule, Split[
        Transpose[{Sort[data], Range[Length[data]]}],
        #1[[1]] == #2[[1]] &], {2}]], {1}]

         {3, 2, 3, 5, 1}

In some cases the rules used might be useful ( I use First/@ rather than
Flatten to reduce the length of the list)

First /@ Apply[Rule,
    Split[Transpose[{Sort[data],
          Range[Length[data]]}], #1[[1]] == #2[[1]] &], {2}]

        {1 -> 1, 7 -> 2, 15 -> 3, 20 -> 5}

A variant on the coding:

Replace[data,Flatten[Split[
MapIndexed[Rule[#1,#2[[1]]]&,Sort[data]],#1[[1]]==#2[[1]]&]],{1}]

        {3, 2, 3, 5, 1}

And

First /@ Split[
    MapIndexed[Rule[#1, #2[[1]]] &, Sort[data]], #1[[1]] == #2[[1]] &]

        {1 -> 1, 7 -> 2, 15 -> 3, 20 -> 5}


--
Allan
---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565

"Wen-Feng Hsiao" <d8442803 at student.nsysu.edu.tw> wrote in message
news:8djk08$7dc at smc.vnet.net...
> Dear all,
>
> I would like to obtain the ranking of a list of elements. So I conduct
> the following procedure:
>
> In[60]:=
> pseudo = {15, 7, 15, 1}
> pseudoSort = pseudo // Sort
> ranks = Position[pseudoSort, #] & /@ pseudo
> rmDuplicate[ranks] // Flatten
>
> Out[60]=
> {15, 7, 15, 1}
> Out[61]=
> {1, 7, 15, 15}
> Out[62]=
> {{{3}, {4}}, {{2}}, {{3}, {4}}, {{1}}}
> Out[63]=
> {3, 2, 4, 1}
>
> However, there are two questions: 1) the ranking is not quite correct, in
> which the tie situation is not addressed well in my procedure. Is
> there any system defined function for this purpose? And, 2) the function
> rmDuplicate is written in procedural way, i.e., using For to remove
> duplicated elements, could someone help/show me using functional or rule
> way to design this function? Thanks for your help.
>
> Wen-Feng
>




  • Prev by Date: Re: Vector Union
  • Next by Date: RE: Piecewise functions
  • Previous by thread: Inverting a matrix with symbolic bounds
  • Next by thread: Re: how to rank a list of elements?