MathGroup Archive 2010

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

Search the Archive

Re: Re: Question about Mathematica

  • To: mathgroup at smc.vnet.net
  • Subject: [mg106592] Re: [mg106571] Re: Question about Mathematica
  • From: Carl Woll <carlw at wolfram.com>
  • Date: Sun, 17 Jan 2010 07:13:02 -0500 (EST)
  • References: <hipld8$7po$1@smc.vnet.net> <201001161114.GAA21878@smc.vnet.net>

Peter.Pein wrote:
> "Dominic" <miliotodc at rtconline.com> schrieb im Newsbeitrag 
> news:hipld8$7po$1 at smc.vnet.net...
>   
>> Hi.  Can someone help me with the following question:
>>
>> I have a table of a table of number pairs:
>>
>> {{{1,2),(3,-1),{2,-4)},{{1,2},{4,-5},{6,-8}},{{2,-1},{-2,-3},{-4,6}}}
>>
>> How may I find the minimum second element in each sub-table?  For
>> example, the first sub-table is:
>>
>> {{1,2},{3,-1},{2,-4}}
>>
>> I would like to then extract the {2,-4} element from this item.  Then
>> the {6,-8} from the second sub-table, then the {-2,-3} element from the
>> last.
>>
>> Thank you,
>> Dominic
>>
>>     
>
>
> Hi Domonic,
>
> test3 = Table[row[[Ordering[row[[All, 2]]][[1]]]], {row, #}] &
>
> is the fastest solution I found so far.
> The others have been
>
> test1 = (Cases[#1, {_, Min[#1[[All, 2]]]}, 1, 1][[1]] &) /@ # &
>
> and the slow
>
> test2 = Fold[If[#2[[2]] < #1[[2]], #2, #1] &, {foo, Infinity}, #] & /@ # &
>
> All three give the desired result for your example and with
> bigdat = RandomInteger[{-9, 9}, {10^6, 10, 2}];
> I get
>
> In[12]:= Timing[result1=test1[bigdat];][[1]]
> Out[12]= 19.406
> In[13]:= Timing[result2=test2[bigdat];][[1]]
> Out[13]= 46.738
> In[14]:= Timing[result3=test3[bigdat];][[1]]
> Out[14]= 8.112
> In[15]:= SameQ[result1,result2,result3]
> Out[15]= True
>
> Peter
>  
>   
Slightly faster would be to use Ordering[..., 1] instead of just 
Ordering[...].

Carl Woll
Wolfram Research


  • Prev by Date: Re: Mathematica Player
  • Next by Date: Testing Mathematica Expressions?
  • Previous by thread: Re: Question about Mathematica
  • Next by thread: Re: Converting date strings to DateList format - a need