Re: Part isn't recursive?
- To: mathgroup at smc.vnet.net
- Subject: [mg119155] Re: Part isn't recursive?
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Mon, 23 May 2011 06:26:01 -0400 (EDT)
On 5/22/11 at 6:55 AM, berniethejet at gmail.com (BernieTheJet) wrote: >I was a little surprised, after all these years, to discover that >Part doesn't automatically recurse over lists of lists of >indexes. Doesn't that seem like an obvious ability? >So, for example, >X = Table[i + j, {i, 3}, {j, 6}] {{2, 3, 4, 5, 6, 7}, {3, 4, 5, 6, >7, 8}, {4, 5, 6, 7, 8, 9}} >X[[All, {1, 2, 3}]] {{2, 3, 4}, {3, 4, 5}, {4, 5, 6}} >X[[All, {4, 5, 6}]] {{5, 6, 7}, {6, 7, 8}, {7, 8, 9}} >X[[All, {{1, 2, 3}, {4, 5, 6}}]] Part::pspec: Part specification >{{1,2,3},{4,5,6}} is neither an integer nor a list of integers. >> >Clearly, what I had hoped for was: {{{2, 3, 4}, {3, 4, 5}, {4, 5, >6}}, {{5, 6, 7}, {6, 7, 8}, {7, 8, 9}}} Easily done as: In[19]:= x[[All, #]] & /@ {{1, 2, 3}, {4, 5, 6}} Out[19]= {{{2, 3, 4}, {3, 4, 5}, {4, 5, 6}}, {{5, 6, 7}, {6, 7, 8}, {7, 8, 9}}} >I mean now I have to resort to an inelegant programming form that I >thought I had left behind with Mathematica: >Table[X[[All, i]], {i, {{1, 2, 3}, {4, 5, 6}}}] Certainly that works too. But it clearly isn't the only solution. As to why Part doesn't work the way you would like, all I can say is that isn't the way Part was designed. It seems to me there is little point in asking why Mathematica doesn't work differently. It is far more useful to understand how Mathematica works and can be used to achieve what you want.