       • 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]]][]]], {row, #}] &
>
> is the fastest solution I found so far.
> The others have been
>
> test1 = (Cases[#1, {_, Min[#1[[All, 2]]]}, 1, 1][] &) /@ # &
>
> and the slow
>
> test2 = Fold[If[#2[] < #1[], #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:= Timing[result1=test1[bigdat];][]
> Out= 19.406
> In:= Timing[result2=test2[bigdat];][]
> Out= 46.738
> In:= Timing[result3=test3[bigdat];][]
> Out= 8.112
> In:= SameQ[result1,result2,result3]
> Out= 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?