       Value and position in list

• To: mathgroup at smc.vnet.net
• Subject: [mg33403] Value and position in list
• From: Hugh Goyder <goyder at rmcs.cranfield.ac.uk>
• Date: Wed, 20 Mar 2002 01:53:08 -0500 (EST)
• Sender: owner-wri-mathgroup at wolfram.com

```I have a list where each element is a list of three values
{{v11,v12,v13},{v21,v22,v23}...}.
What is the fastest method for finding the position and value of the
element which has the smallest first element.

I have the following but is it possible to do this more quickly, perhaps
without two sorts through the data?

In:=
\$Version

Out=
4.1 for Microsoft Windows (June 13, 2001)

In:=
data=Table[{Random[],Random[]-0.5,10(Random[]-0.5)},{10000}];

In:=
Timing[a=Min[Transpose[data][]];p=Position[data,a];{a,p}]

Out=
{0.33 Second,{0.0000316723,{{1706,1}}}}

I must also find the element which has the smallest second element greater
than zero. Here are my attempts so far. Is there a faster method?

In:=
Timing[a=Infinity;i=0;
While[i++;i<Length[data],If[0<data[[i,2]]<a,a=data[[i,2]];p=i]];{a,p}]

Out=
{1.31 Second,{0.0000126855,1134}}

In:=
Timing[a=Min[Transpose[data][]/.v_/;v\[LessEqual] 0 \[Rule] Infinity];
p=Position[data,a];{a,p}]

Out=
{0.71 Second,{0.0000126855,{{1134,2}}}}

In:=
Timing[a=Min[Select[Transpose[data][],#>0&]];p=Position[data,a];{a,p}]

Out=
{0.61 Second,{0.0000126855,{{1134,2}}}}