Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #2633

Re: Part isn't recursive?

From Bill Rowe <readnews@sbcglobal.net>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: Part isn't recursive?
Date 2011-05-23 10:25 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <irdcnk$3o5$1@smc.vnet.net> (permalink)

Show all headers | View raw


On 5/22/11 at 6:55 AM, berniethejet@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.

Back to comp.soft-sys.math.mathematica | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Re: Part isn't recursive? Bill Rowe <readnews@sbcglobal.net> - 2011-05-23 10:25 +0000
  Re: Part isn't recursive? "p.ramsden" <p.ramsden@imperial.ac.uk> - 2011-05-24 09:59 +0000
    Re: Part isn't recursive? BernieTheJet <berniethejet@gmail.com> - 2011-05-25 09:57 +0000

csiph-web