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