5
$\begingroup$

Perhaps a very easy question but I posted this on the Wolfram Community and didn't get any answer..

Suppose I have data like:

data = Table[{{Subscript[a, x, 1, 1], Subscript[a, x, 1, 2], 
    Subscript[a, x, 1, 3]}, {Subscript[b, x, 2, 1], Subscript[b, x, 2,
     2], Subscript[b, x, 2, 3]}}, {x, 1, 3}]

enter image description here

Now suppose I wan't to get from all rows the first item in the first list and the third item in the second list like:

enter image description here

It can be done by using:

{data[[All, 1, 1]], data[[All, 2, 3]]}\[Transpose]

or

{#[[1, 1]], #[[2, 3]]} & /@ data

But I'm wondering if there isn't an easier way by only using Part,Extract? It seems that going through the documentation the option only apply to one column (or more) but not to subparts in these columns.

Any thoughts?

$\endgroup$
3
  • $\begingroup$ this Q&A is helpful and may have what you seek. $\endgroup$ Commented Oct 24, 2014 at 15:07
  • 1
    $\begingroup$ {#1[[1]], #2[[-1]]} & @@@ data $\endgroup$
    – Junho Lee
    Commented Oct 24, 2014 at 15:20
  • $\begingroup$ data /. {{f_, __}, {__, l_}} -> {f, l} $\endgroup$
    – Junho Lee
    Commented Oct 24, 2014 at 15:38

3 Answers 3

3
$\begingroup$

I don't think there is any way to do it using just Part or Extract. With these functions you specify the elements you want at level 1, then at level 2, then level 3 etc. At each level you may specify a single element, a list of elements, All or a Span. There is no syntax for specifying different sub-parts for each of the elements selected at a certain level.

For an alternative, I suggest using the operator form of Extract along with Map:

Extract[{{1, 1}, {2, 3}}] /@ data
$\endgroup$
1
  • $\begingroup$ +1 good explanation. and also it is same of Extract[{{1, 1}, {-1, -1}}] /@ data $\endgroup$
    – Junho Lee
    Commented Oct 24, 2014 at 22:49
6
$\begingroup$

My favorite being a slight variation of @Lou's own solution

Transpose[{data[[;;, 1, 1]], data[[;;, -1, -1]]}]

you can also use

Transpose@Rest@Extract[data, {{}, {;; , 1, 1}, {;; , -1, -1}}]
(* version 9 only -- does not work in version 10 -- thanks: Simon Woods *)

or

ReplacePart[data, p_ :> {data[[p, 1, 1]], data[[p, -1, -1]]}]

or

{First@First@#, Last@Last@#} & /@ data

or

ClearAll[f];
f[{{a_, __}, {__, b_}}] := {a, b}
f /@ data

all give

enter image description here

$\endgroup$
2
  • $\begingroup$ What version are you using? Your second one does not work for me (I get a "Position specification ... is not applicable" error) $\endgroup$ Commented Oct 24, 2014 at 20:13
  • $\begingroup$ @Simon I am using V 9.0.1 (Windows 8 64-bit). $\endgroup$
    – kglr
    Commented Oct 24, 2014 at 20:23
4
$\begingroup$

My try

{#1[[1]], #2[[-1]]} & @@@ data

or

data /. {{f_, __}, {__, l_}} -> {f, l}
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.