MathGroup Archive 1992

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

Search the Archive

Position of Max

  • To: mathgroup <mathgroup at>
  • Subject: Position of Max
  • From: HAY at
  • Date: Sat, 23 MAY 92 22:10:37 GMT

Athanasios Orphanides  writes:

> I am trying to find the most efficient way to get the maximum element
> in a list AND its first position in the list. This is done
> a few thousand times in an iteration program so any saving would
> be useful. Currently I use Max and Position. For example:
>    x=Table[100 i^2-i^3, {i,2000}]
>    Timing[xm=Max[x]; Position[x,xm][[1,1]]]
> Is there a way to retrieve the position by just doing Max[]
> and identifying the index of the result without having to use
> Position[]?

We can get a modest speedup by using the extra slots available in Position:
Position[x, xm, {1}, 1] searches only at level 1 and return the position of 
the first occurrence of xm that is found,

x=Table[100 i^2-i^3, {i,2001}];
Timing[xm=Max[x]; Position[x,xm,{1},1][[1,1]] ]
x=Table[100 i^2-i^3, {i,2000}];
Timing[xm=Max[x]; Position[x,xm][[1,1]]]

{0.75 Second, 67}

{0.883333 Second, 67}

Allan Hayes
Department of Mathematics
The University
Leicester LE1 7RH
hay at

  • Prev by Date: Draft tutorial on TeX/Mathematica
  • Next by Date: [no subject]
  • Previous by thread: Position of Max
  • Next by thread: polygon problem in 3D