Message Boards Message Boards


Using Part to access a portion of an irregular array ...

Posted 9 years ago
2 Replies
0 Total Likes

Here's an irregular array (a 3-row sample of many thousands of rows) ...

In[3268]:= {{{a, b}, c}, {{d, e}, f}, {{g, h}, i}}

Out[3268]= {{{a, b}, c}, {{d, e}, f}, {{g, h}, i}}

Here's part of what I want ...

In[3269]:= %[[All, 1, 2]]

Out[3269]= {b, e, h}

How do I get with either a single Part command or a sequence of them ... ?


Here's a simple work-around ...

In[4087]:= {{{a, b}, c}, {{d, e}, f}, {{g, h}, i}}

Out[4087]= {{{a, b}, c}, {{d, e}, f}, {{g, h}, i}}

In[4089]:= (Flatten /@ %)[[All, 2 ;; 3]]

Out[4089]= {{b, c}, {e, f}, {h, i}}

But it will still be nice to do this with a single invocation of Part ...

As this is in an inner portion of a computation I'm trying to avoid multiple steps and achieve a "functional" solution.

Thank you, -- Mark

POSTED BY: Mark Tuttle
2 Replies

Perfect. Just what I wanted.

And a nice teachable moment. (I keep forgetting about Apply ... To many years of procedural programming.)

-- Mark

POSTED BY: Mark Tuttle

Using simply the function Part: you can't, simple as that!

you could using something like:

{#1[[2]],#2}&@@@ {{{a, b}, c}, {{d, e}, f}, {{g, h}, i}}

or the Flatten you proposed or some variation...

POSTED BY: Sander Huisman
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract