Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2000
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

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: [mg23226] Re: how to rank a list of elements?
  • From: "Allan Hayes" <hay at haystack.demon.co.uk>
  • Date: Tue, 25 Apr 2000 01:40:30 -0400 (EDT)
  • References: <8djk08$7dc@smc.vnet.net> <8dnuge$hrl@smc.vnet.net> <8e0m7u$f1a@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Wen-Feng,
Using Position as in your code for the function ranking below seems to be
rather slow. I give a variant, ranking2, of  my original posting that is
much faster on the test that I have used. I have suppressed the printing of
the rules in your code.

(*ties = -1, low; ties = 0, mean; ties = 1, high*)
ranking[datalst_List, ties_:0] :=
    Module[{ratings, posset, substitutes}, methods = {Min, Mean, Max};
      posset =
        Position[Sort[datalst], #] & /@ Sort[datalst] /. {lst_} -> lst;
      ratings = methods[[ties + 2]][#] & /@ posset // N;
      substitutes = Apply[Rule, Transpose[{Sort[datalst], ratings}], {1}];
      (*Print[substitutes];*)
      Return[datalst /. substitutes];];


ranking2[data_, method_] :=
  data /. (#[[1, 1]] -> method[N[#[[All, 2]]]] & /@
        Split[Transpose[{Sort[data],
Range[Length[data]]}],
#1[[1]] == #2[[1]] &])

Tests

<< Statistics`DescriptiveStatistics`

data = Table[Random[Integer, 10] // N, {200}];

(r1 = ranking[data]); // Timing // First

1.65 Second

(r2 = ranking2[data, Mean]); // Timing // First

0.05 Second

r1 === r2

True

You have probably noticed that the use of Split in my earlier posting was
unnecessary (for ranking with method Min)


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






  • Prev by Date: Re: Demonstrate that 1==-1
  • Next by Date: fastest way to do pair-sum / make pair-list
  • Previous by thread: Re: Re: how to rank a list of elements?
  • Next by thread: Gaussian fit