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

Posted 9 years ago
4389 Views
|
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 ... ?

[{b,c},{e,f},{h,i}}

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

2 Replies
Sort By:
Posted 9 years ago
 Perfect. Just what I wanted.And a nice teachable moment. (I keep forgetting about Apply ... To many years of procedural programming.)-- Mark
Posted 9 years ago
 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...