MathGroup Archive 2004

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

Search the Archive

Re: Label of Max[list]


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.

Bobby

On Thu, 19 Aug 2004 06:28:21 -0400 (EDT), Dr. Wolfgang Hintze <weh at snafu.de> wrote:

> Is there a standard function providing beside the maximum of a list also
> the label(s) of the maximum?
>
> My solution is this
>
> In[23]:=
> wehMax[li_] := {m = Max[li], Select[Range[Length[li]],
>      li[[#1]] == m & ]}
>
> In[24]:=
> li = Table[Random[], {100}];
>
> In[25]:=
> wehMax[li]
>
> Out[25]=
> {0.9963517693166272, {89}}
>
> Is there a better one?
>
> Any hint appreciated.
>
> Wolfgang
>
>
>



-- 
DrBob at bigfoot.com
www.eclecticdreams.net


  • Prev by Date: 3D Screensavers for Mathematica users
  • Next by Date: Re: Hypergeometric function
  • Previous by thread: Re: Label of Max[list]
  • Next by thread: Re: Label of Max[list]