From: Peter Breitfeld on 16 Jan 2010 06:14 "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 > > This is a possibility: ll = {{{1, 2}, {3, -1}, {2, -4}}, {{1,2}, {4, -5}, {6, -8}}, {{2, -1}, {-2, -3}, {-4, 6}}} minsecond[l:{{_,_}..}]:=Sort[l,#1[[2]]<#2[[2]]&] First /@ (minsecond /@ ll) Out= {{2, -4}, {6, -8}, {-2, -3}} -- _________________________________________________________________ Peter Breitfeld, Bad Saulgau, Germany -- http://www.pBreitfeld.de
From: Carl Woll on 17 Jan 2010 07:12 Peter.Pein wrote: > "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 > > Slightly faster would be to use Ordering[..., 1] instead of just Ordering[...]. Carl Woll Wolfram Research
First
|
Prev
|
Pages: 1 2 3 Prev: NMaximize: Method selection impossible Next: Hessian Matrix of a Numerical Defined Function |