       Re: Value and position in list

• To: mathgroup at smc.vnet.net
• Subject: [mg33431] Re: [mg33403] Value and position in list
• From: BobHanlon at aol.com
• Date: Thu, 21 Mar 2002 09:27:11 -0500 (EST)
• Sender: owner-wri-mathgroup at wolfram.com

```In a message dated 3/20/02 4:27:22 AM, goyder at rmcs.cranfield.ac.uk writes:

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

\$Version

4.1 for Mac OS X (November 5, 2001)

data=
Table[{Random[],Random[]-0.5,10(Random[]-0.5)},
{50000}];

First problem

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

{0.3500000000000014*Second, {0.00001864319190335675,

{{1017, 1}}}}

Timing[a=Min[d=data[[All,1]]];
p=(Join[#,{1}]&/@Position[d,a]);{a,p}]

{0.09999999999999432*Second, {0.00001864319190335675,

{{1017, 1}}}}

Second problem

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

{0.6600000000000037*Second, {4.325948545713665*^-6,

{{44293, 2}}}}

Timing[a=Min[Select[d=data[[All,2]],#>0&]];
p=(Join[#,{1}]&/@Position[d,a]);{a,p}]

{0.4299999999999997*Second, {4.325948545713665*^-6,

{{44293, 1}}}}

Bob Hanlon
Chantilly, VA  USA

```

• Prev by Date: Re: RE: Re: newbie question - printing Pi
• Next by Date: Re: newbie question - plot
• Previous by thread: Re: Value and position in list
• Next by thread: RE: Value and position in list