Re: Label of Max[list]
- To: mathgroup at smc.vnet.net
- Subject: [mg50226] Re: Label of Max[list]
- From: Bill Rowe <readnewsciv at earthlink.net>
- Date: Sat, 21 Aug 2004 03:04:40 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
On 8/20/04 at 4:57 AM, drbob at bigfoot.com (DrBob) wrote: >wehMax[li_] := {m = Max[li], Select[Range[Length[li]], li[[#1]] == m & ]} >myLabelMax[data_] := > Module[{max = Max[data]}, {max, Flatten[Position[data, max]]}] >test[n_] := Module[ > {data = RandomArray[BinomialDistribution[10, 0.3], {n}], one, two}, > one = Timing[wehMax[data]]; > two = Timing[myLabelMax[data]]; > {First[one], First[two], Last[one] == Last[two]}] >test[1000000] >{1.5779999999999994*Second, 0.09299999999999997*Second, True} >That's a timing for your method, then mine, and a test to see if >they got the same answer. If it is speed you are after consider labelMax[data_] := ({data[[#1[[1]]]], #1} & )[Ordering[data, -1]] test[n_] := Module[ {data = Table[Random[], {n}], one, two}, one = Timing[labelMax[data]]; two = Timing[myLabelMax[data]]; {First[one], First[two], Last[one] == Last[two]}] test[1000000] {0.03999999999999915*Second, 1.3100000000000023*Second, True} Note, I changed your test program to use a uniform distribution of reals rather than a binomial distribution of integers for two reasons. First, generating a million uniform reals requires less time then generating a million binomial integers. Second, it is very likely the maximum of a million uniform reals is a unique value in that set. If the maximum value is not unique then the two methods will not give the same result. Flatten@Position[data,Max@data] will return an ordered lists of all indices where Max@data occurs Ordering[data, -1] will return a one element list containing the position of the last occurance of Max@data Your method will get the same results as the method used by the OP. It isn't clear whether the OP considered duplicate maximum values or not. -- To reply via email subtract one hundred and four