MathGroup Archive 2002

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

Search the Archive

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



Thank you for your ideas

Hugh Goyder




  • Prev by Date: Re: Creating List of Sequence Iterates
  • Next by Date: Re: Mathlink for Excel
  • Previous by thread: help!! Clueless on how to proceed...
  • Next by thread: Re: Value and position in list