MathGroup Archive 2002

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

Search the Archive

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[1]:=
>$Version
>
>Out[1]=
>4.1 for Microsoft Windows (June 13, 2001)
>
>In[2]:=
>data=Table[{Random[],Random[]-0.5,10(Random[]-0.5)},{10000}];
>
>In[3]:=
>Timing[a=Min[Transpose[data][[1]]];p=Position[data,a];{a,p}]
>
>Out[3]=
>{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[4]:=
>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[4]=
>{1.31 Second,{0.0000126855,1134}}
>
>In[5]:=
>Timing[a=Min[Transpose[data][[2]]/.v_/;v\[LessEqual] 0 \[Rule] Infinity];
>  p=Position[data,a];{a,p}]
>
>Out[5]=
>{0.71 Second,{0.0000126855,{{1134,2}}}}
>
>In[6]:=
>Timing[a=Min[Select[Transpose[data][[2]],#>0&]];p=Position[data,a];{a,p}]
>
>Out[6]=
>{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][[1]]];
  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][[2]],#>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