From: Dominic on
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


From: Leonid Shifrin on
Hi Dominic,

here is your data:

data = {{{1, 2}, {3, -1}, {2, -4}}, {{1,
2}, {4, -5}, {6, -8}}, {{2, -1}, {-2, -3}, {-4, 6}}};

Here is one way:

In[2]:=
Flatten[Extract[#, Position[#, Min[#], 1, 1] &[#[[All, 2]]]] & /@ data, 1]

Out[2]= {{2, -4}, {6, -8}, {-2, -3}}

Here is another one:

In[3]:= Map[SortBy[#, Last] &, data][[All, 1]]

Out[3]= {{2, -4}, {6, -8}, {-2, -3}}

This one is shorter but the first one is faster for larger lists. Since
there may be many sublists with the same minimal second element, the two
methods will not necessarily give identical results.

Regards,
Leonid


On Fri, Jan 15, 2010 at 3:00 PM, Dominic <miliotodc(a)rtconline.com> wrote:

> 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
>
>
>
From: Norbert P. on
Hi Dominic,

use Ordering with Part or Extract:

In[1]:= t={{{1,2},{3,-1},{2,-4}},{{1,2},{4,-5},{6,-8}},{{2,-1},{-2,-3},
{-4,6}}};

In[2]:= #[[First(a)Ordering[#,{2}]]]&/@t
Out[2]= {{2,-4},{4,-5},{-2,-3}}

In[3]:= Extract[#,Ordering[#,{2}]]&/@t
Out[3]= {{2,-4},{4,-5},{-2,-3}}

Even Sort will do:

In[4]:= Sort[#][[2]]&/@t
Out[4]= {{2,-4},{4,-5},{-2,-3}}

Best,
Norbert

On Jan 15, 4:00 am, "Dominic" <miliot...(a)rtconline.com> wrote:
> 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


From: Tomas Garza on
Assuming there is a single minimum in each sub-table (otherwise, the problem must be specified more precisely):

In[1]:= mat={{{1,2},{3,-1},{2,-4}},{{1,2},{4,-5},{6,-8}},{{2,-1},{-2,-3},{-4,6}}};

In[2]:= indpos=Flatten[Position[#,{u_,v_}/;v==Min[Cases[#,{x_,y_}->y]]]&/@mat]
Out[2]= {3,3,2}

In[3]:= MapThread[Part,{mat,indpos}]
Out[3]= {{2,-4},{6,-8},{-2,-3}}

(BTW, you have mixed ordinary parentheses and curly brackets in your example; I corrected them).

Tomas

> Date: Fri, 15 Jan 2010 07:00:46 -0500
> From: miliotodc(a)rtconline.com
> Subject: Question about Mathematica
> To: mathgroup(a)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
>
>


From: DC on
In[1]:= alist = {{{1, 2}, {3, -1}, {2, -4}}, {{1,
2}, {4, -5}, {6, -8}}, {{2, -1}, {-2, -3}, {-4, 6}}};


In[2]:= SortBy[#, Last][[1]] & /@ alist

Out[2]= {{2, -4}, {6, -8}, {-2, -3}}

-Francesco

On 01/15/2010 12:00 PM, Dominic wrote:
> 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
>
>