From: DrMajorBob on 16 Jan 2010 06:12 data = {{{1, 2}, {3, -1}, {2, -4}}, {{1, 2}, {4, -5}, {6, -8}}, {{2, -1}, {-2, -3}, {-4, 6}}}; Reverse /@ (Sort /@ Map[Reverse, data, {2}])[[All, 1]] {{2, -4}, {6, -8}, {-2, -3}} or (SortBy[#, Last] & /@ data)[[All, 1]] {{2, -4}, {6, -8}, {-2, -3}} Bobby On Fri, 15 Jan 2010 06:00:46 -0600, 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 > > -- DrMajorBob(a)yahoo.com
From: Bob Hanlon on 16 Jan 2010 06:12 data = { {{1, 2}, {3, -1}, {2, -4}}, {{1, 2}, {4, -5}, {6, -8}}, {{2, -1}, {-2, -3}, {-4, 6}}}; SortBy[#, Last][[1]] & /@ data {{2, -4}, {6, -8}, {-2, -3}} Cases[#, {_, Min[#[[All, -1]]]}][[1]] & /@ data {{2, -4}, {6, -8}, {-2, -3}} f[x_] := Select[x, #[[-1]] == Min[x[[All, -1]]] &] First /@ f /@ data {{2, -4}, {6, -8}, {-2, -3}} Bob Hanlon ---- 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: Francisco Javier Chávez Contreras on 16 Jan 2010 06:12 Hello Dominic, im use a "For" for solving your trouble: --------------------------------------------------------------------------- --------------------------------------------- tablein = {{{1, 2}, {3, -1}, {2, -4}}, {{1, 2}, {4, -5}, {6, -8}}, {{2, -1}, {-2, -3}, {-4, 6}}}; mintable = {}; For[i = 1, i <= Length[tablein], i++, mini = Min[Drop[tablein[[i]], {}, {1}]]; posi = Position[tablein[[i]], mini][[1, 1]]; mintable = Append[mintable, Extract[tablein[[i]], posi]]; ]; mintable ------------------------------------------------------------------------------------------------------------------------- I hope this code will serve you, greetings. Regards, Francisco Ch=E1vez Contreras Santiago, Chile 2010/1/15 Dominic <miliotodc(a)rtconline.com> > 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: Peter.Pein on 16 Jan 2010 06:13 "Dominic" <miliotodc(a)rtconline.com> schrieb im Newsbeitrag news:hipld8$7po$1(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 > 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
From: Albert Retey on 16 Jan 2010 06:13 Hi, > > 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 One possibility: Map[SortBy[#, Last][[1]] &, data] hth, albert
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 Prev: NMaximize: Method selection impossible Next: Hessian Matrix of a Numerical Defined Function |