Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2001
*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 2001

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

Search the Archive

Re: Greatest element in list

  • To: mathgroup at smc.vnet.net
  • Subject: [mg30524] Re: Greatest element in list
  • From: "Allan Hayes" <hay at haystack.demon.co.uk>
  • Date: Fri, 24 Aug 2001 20:58:17 -0400 (EDT)
  • References: <9m27in$gqd$1@smc.vnet.net> <9m5267$qe0$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Jens,
Your function MaxPosition seems to be nearly twice as fast as
Position[lst,{1},1] unless a maximum entry comes very early in the list
(much of the speed seems to be due to the compiling).
Howevver the new function Ordering is over ten times faster.

MaxPosition=  (*Jens-Peer Kuska*)
    Compile[{{lst,_Real,1}},
      Module[{max=First[lst],pos=1,i=2},
        Scan[If[#>max,max=#;pos=i++,i++]&,Rest[lst]];
        pos]];


MaxPosition2[lst_]:= (*uncompiled version of above*)
    Module[{max=First[lst],pos=1,i=2},
      Scan[If[#>max,max=#;pos=i++,i++]&,Rest[lst]];
      pos];

TIMINGS

lst=Insert[Table[Random[],{100000}],2.0,50000];

Timing[Position[#,Max[#]]&[lst]]

        {1.43 Second,{{50000}}}

Timing[Position[#,Max[#],{1},1]&[lst]]

        {1.15 Second,{{50000}}}

MaxPosition[lst]//Timing

        {0.66 Second,50000}

Timing[MaxPosition[lst]]

        {7.2 Second,714995}

Timing[MaxPosition2[lst]]

        {24.45 Second,50000}

Ordering[lst,-1]//Timing

        {0.11 Second,{50000}}

--
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

"Jens-Peer Kuska" <kuska at informatik.uni-leipzig.de> wrote in message
news:9m5267$qe0$1 at smc.vnet.net...
> Hi,
>
> without your data I can't reproduce the problem.
> But
>
> MaxPosition = Compile[{{lst, _Real, 1}}, Module[{max = First[lst], pos =
> 1, i = 2},
>       Scan[
>         If[# > max, max = #; pos = i++, i++] & ,
>         Rest[lst]
>         ];
>       pos
>       ]
>     ]
>
> is atleast faster than Position[#, Max[#]] &[lst]
>
> In[]:=lst = Table[Random[], {1000000}];
>
> In[]:=Timing[Position[#, Max[#]] &[lst]]
> Out[]={6.85 Second, {{602020}}}
>
> In[]:=Timing[MaxPosition[lst]]
> Out[]={4.42 Second, 602020}
>
> Because it scans the list only once.
>
> Regards
>   Jens
>
>
> Oliver Friedrich wrote:
> >
> > Hi,
> >
> > what's the best way to get the position of the greatest number in list
of
> > reals? I've tried
> >
> > Position[#,Max[#]]&list
> >
> > but surprisingly, it doesn't work all the time, sometimes it returns an
> > empty list. How is that, because a theorem says that a non empty set of
real
> > numbers must have at least one biggest element. So Max[#] can't be
empty.
> >
> > Any solutions ?
> >
> > Oliver Friedrich
>




  • Prev by Date: RE: Re: Curl
  • Next by Date: Re: Fitting to a square wave
  • Previous by thread: RE: Greatest element in list
  • Next by thread: Re: Greatest element in list