From: Bill Rowe on 3 Mar 2010 05:53 On 3/2/10 at 3:36 AM, graser(a)gmail.com (graser) wrote: >I want to pick up the maximum value of each list in the Array >A={{2, 4, 1, 3, 2, 2}, {4,7,9,1,2,4}} >I can do it in two steps.. >B=Table[Sort[A[[i]], #1>#2&], {i, 1, Length[A]}]; >C=Table[B[i], {i, 1, Length[B]}]; >But is there any way I can do it in just one step with Select >function or any thing else? There are several problems with your code. First, it is unwise to use uppercase letters as variables since in several cases that will conflict with built-in symbols. In particular an upper case C has a built-in meaning. You have defined b to be a 2-D array. Yet the code for setting the value of c treats b as a function. I assume you meant the following where I have changed your variables to lower case: a = {{2, 4, 1, 3, 2, 2}, {4, 7, 9, 1, 2, 4}}; b = Table[Sort[a[[i]], #1 > #2 &], {i, 1, Length[a]}]; c = Table[b[[i]], {i, 1, Length[b]}]; Note, the code here that creates b is an identity function. The following does the same thing in one step In[4]:= d = Reverse[Sort@#] & /@ a; {d == b, d == c} Out[5]= {True,True} But this clearly does not find the maximum value of each list. That would be done as In[6]:= Max /@ a Out[6]= {4,9} Or perhaps you wanted this In[7]:= Max /@ Transpose[a] Out[7]= {4,7,9,3,2,4}
From: graser on 3 Mar 2010 05:54 On Mar 2, 8:01 am, Patrick Scheibe <psche...(a)trm.uni-leipzig.de> wrote: > Hi, > > it is simply > > A = {{2, 4, 1, 3, 2, 2}, {4, 7, 9, 1, 2, 4}}; > > Max /@ A > > and your code is not working at all. > > Cheers > Patrick > > Am Mar 2, 2010 um 9:36 AM schrieb graser: > > > > > Hi > > > I want to pick up the maximum value of each list in the Array > > > A={{2, 4, 1, 3, 2, 2}, {4,7,9,1,2,4}} > > > I can do it in two steps.. > > > B=Table[Sort[A[[i]], #1>#2&], {i, 1, Length[A]}]; > > > C=Table[B[i], {i, 1, Length[B]}]; > > > But is there any way I can do it in just one step with Select function > > or any thing else? > > > Thanks Thanks for all of your comments.. Here I have more question for you.. If Array has A={{1,2},{2,1},{3,4}},{{1,5},{2,1},{3,2}}.. Then If I want to pick up the list which has maximum value in second column, So my result should be {{3,4},{1,5}}, can I still use the Max? It just give the maximum value in first column like {4,5} Or I have to use the multiple steps like B = Table[Sort[A[[i]], #1[[2]] > #2[[2]] &], {i, 1, Length[A]}]; CD = Table[B[[i, 1]], {i, 1, Length[B]}];
From: Bob Hanlon on 4 Mar 2010 05:25 A = {{{1, 2}, {2, 1}, {3, 4}}, {{1, 5}, {2, 1}, {3, 2}}}; SortBy[#, Last][[-1]] & /@ A {{3, 4}, {1, 5}} Bob Hanlon ---- graser <graser(a)gmail.com> wrote: ============= On Mar 2, 8:01 am, Patrick Scheibe <psche...(a)trm.uni-leipzig.de> wrote: > Hi, > > it is simply > > A = {{2, 4, 1, 3, 2, 2}, {4, 7, 9, 1, 2, 4}}; > > Max /@ A > > and your code is not working at all. > > Cheers > Patrick > > Am Mar 2, 2010 um 9:36 AM schrieb graser: > > > > > Hi > > > I want to pick up the maximum value of each list in the Array > > > A={{2, 4, 1, 3, 2, 2}, {4,7,9,1,2,4}} > > > I can do it in two steps.. > > > B=Table[Sort[A[[i]], #1>#2&], {i, 1, Length[A]}]; > > > C=Table[B[i], {i, 1, Length[B]}]; > > > But is there any way I can do it in just one step with Select function > > or any thing else? > > > Thanks Thanks for all of your comments.. Here I have more question for you.. If Array has A={{1,2},{2,1},{3,4}},{{1,5},{2,1},{3,2}}.. Then If I want to pick up the list which has maximum value in second column, So my result should be {{3,4},{1,5}}, can I still use the Max? It just give the maximum value in first column like {4,5} Or I have to use the multiple steps like B = Table[Sort[A[[i]], #1[[2]] > #2[[2]] &], {i, 1, Length[A]}]; CD = Table[B[[i, 1]], {i, 1, Length[B]}];
From: DrMajorBob on 4 Mar 2010 05:29 First of all, A={{1,2},{2,1},{3,4}},{{1,5},{2,1},{3,2}}... isn't an array. Or anything else. Secondly, if I set a = {{{1, 2}, {2, 1}, {3, 4}}, {{1, 5}, {2, 1}, {3, 2}}}; a // MatrixForm (eliminating "..." and adding the first and last missing brackets), MatrixForm shows that a is a 3-dimensional matrix, and the second column is a list of pairs. Specifically, the second column is a[[All, 2]] {{2, 1}, {2, 1}} Now, then... how do you want to choose a maximum in that column? Bobby On Wed, 03 Mar 2010 04:54:02 -0600, graser <graser(a)gmail.com> wrote: > On Mar 2, 8:01 am, Patrick Scheibe <psche...(a)trm.uni-leipzig.de> > wrote: >> Hi, >> >> it is simply >> >> A = {{2, 4, 1, 3, 2, 2}, {4, 7, 9, 1, 2, 4}}; >> >> Max /@ A >> >> and your code is not working at all. >> >> Cheers >> Patrick >> >> Am Mar 2, 2010 um 9:36 AM schrieb graser: >> >> >> >> > Hi >> >> > I want to pick up the maximum value of each list in the Array >> >> > A={{2, 4, 1, 3, 2, 2}, {4,7,9,1,2,4}} >> >> > I can do it in two steps.. >> >> > B=Table[Sort[A[[i]], #1>#2&], {i, 1, Length[A]}]; >> >> > C=Table[B[i], {i, 1, Length[B]}]; >> >> > But is there any way I can do it in just one step with Select function >> > or any thing else? >> >> > Thanks > > Thanks for all of your comments.. > > Here I have more question for you.. > > If Array has > > A={{1,2},{2,1},{3,4}},{{1,5},{2,1},{3,2}}.. > > Then If I want to pick up the list which has maximum value in second > column, So my result should be {{3,4},{1,5}}, can I still use the Max? > > It just give the maximum value in first column like {4,5} > > Or I have to use the multiple steps like > > B = Table[Sort[A[[i]], #1[[2]] > #2[[2]] &], {i, 1, Length[A]}]; > CD = Table[B[[i, 1]], {i, 1, Length[B]}]; > -- DrMajorBob(a)yahoo.com
From: Leonid Shifrin on 4 Mar 2010 05:31 Hi, You can not use Max as easily as before for your new formulation. Assuming this is your array: In[18]:= a = {{{1, 2}, {2, 1}, {3, 4}}, {{1, 5}, {2, 1}, {3, 2}}} Out[18]= {{{1, 2}, {2, 1}, {3, 4}}, {{1, 5}, {2, 1}, {3, 2}}} Here are a couple of ways: In[19]:= Module[{max}, Fold[If[#1[[2]] < #2[[2]], max = #2, max] &, max = First@#, Rest@#] & /@ a] Out[19]= {{3, 4}, {1, 5}} In[20]:= Extract[#, First(a)Position[#, {_, Max[#[[All, 2]]]}, 1, 1]] & /@ a Out[20]= {{3, 4}, {1, 5}} Regards, Leonid On Wed, Mar 3, 2010 at 1:54 PM, graser <graser(a)gmail.com> wrote: > On Mar 2, 8:01 am, Patrick Scheibe <psche...(a)trm.uni-leipzig.de> > wrote: > > Hi, > > > > it is simply > > > > A = {{2, 4, 1, 3, 2, 2}, {4, 7, 9, 1, 2, 4}}; > > > > Max /@ A > > > > and your code is not working at all. > > > > Cheers > > Patrick > > > > Am Mar 2, 2010 um 9:36 AM schrieb graser: > > > > > > > > > Hi > > > > > I want to pick up the maximum value of each list in the Array > > > > > A={{2, 4, 1, 3, 2, 2}, {4,7,9,1,2,4}} > > > > > I can do it in two steps.. > > > > > B=Table[Sort[A[[i]], #1>#2&], {i, 1, Length[A]}]; > > > > > C=Table[B[i], {i, 1, Length[B]}]; > > > > > But is there any way I can do it in just one step with Select function > > > or any thing else? > > > > > Thanks > > Thanks for all of your comments.. > > Here I have more question for you.. > > If Array has > > A={{1,2},{2,1},{3,4}},{{1,5},{2,1},{3,2}}.. > > Then If I want to pick up the list which has maximum value in second > column, So my result should be {{3,4},{1,5}}, can I still use the Max? > > It just give the maximum value in first column like {4,5} > > Or I have to use the multiple steps like > > B = Table[Sort[A[[i]], #1[[2]] > #2[[2]] &], {i, 1, Length[A]}]; > CD = Table[B[[i, 1]], {i, 1, Length[B]}]; > >
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 Prev: Annoying error in Hypergeometric2F1 Next: Problem with Simplify and Integrating a gaussian |