Position of Max
- To: mathgroup <mathgroup at yoda.physics.unc.edu>
- Subject: Position of Max
- From: HAY at leicester.ac.uk
- 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} From Allan Hayes Department of Mathematics The University Leicester LE1 7RH U.K. hay at leicester.ac.uk