3
$\begingroup$

Given the following list:

l1={{{1, 4}, {2, 8}, {3, 1}, {4, 9}, {5, 8}, {6, 8}, {7, 5},
{8, 8},{9, 5}, {10, 9}, {11, 1}, {12, 1}}, {{1, 8}, {2, 6}, {3, 6}, {4, 
7}, {5, 7}, {6, 10}, {7, 9}, {8, 6}, {9, 9}, {10, 8}, {11, 
10}, {12, 7}}, {{1, 1}, {2, 2}, {3, 3}, {4, 10}, {5, 1}, {6, 
1}, {7, 1}, {8, 1}, {9, 1}, {10, 2}, {11, 2}, {12, 4}}, {{1, 
6}, {2, 4}, {3, 2}, {4, 3}, {5, 9}, {6, 5}, {7, 4}, {8, 9}, {9, 
7}, {10, 10}, {11, 5}, {12, 6}}, {{1, 5}, {2, 1}, {3, 10}, {4, 
5}, {5, 2}, {6, 2}, {7, 6}, {8, 3}, {9, 4}, {10, 5}, {11, 7}, {12, 
8}}, {{1, 9}, {2, 10}, {3, 7}, {4, 2}, {5, 10}, {6, 3}, {7, 8}, {8,
10}, {9, 8}, {10, 1}, {11, 4}, {12, 10}}, {{1, 3}, {2, 5}, {3, 
5}, {4, 8}, {5, 6}, {6, 6}, {7, 2}, {8, 5}, {9, 10}, {10, 7}, {11, 
6}, {12, 2}}, {{1, 10}, {2, 7}, {3, 8}, {4, 1}, {5, 4}, {6, 7}, {7,
10}, {8, 2}, {9, 3}, {10, 6}, {11, 9}, {12, 5}}, {{1, 7}, {2, 
3}, {3, 9}, {4, 6}, {5, 5}, {6, 4}, {7, 7}, {8, 4}, {9, 6}, {10, 
3}, {11, 8}, {12, 3}}, {{1, 2}, {2, 9}, {3, 4}, {4, 4}, {5, 3}, {6,
9}, {7, 3}, {8, 7}, {9, 2}, {10, 4}, {11, 3}, {12, 9}}};

How can I create this list:

l2={{4, 8, 1, 6, 5, 9, 3, 10, 7, 2}, {8, 6, 2, 4, 1, 10, 5, 7, 3, 9}, {1,
6, 3, 2, 9, 7, 5, 8, 9, 4}, {9, 7, 10, 3, 5, 2, 8, 1, 6, 4}, {8, 7,
1, 9, 2, 10, 6, 4, 5, 3}, {8, 10, 1, 5, 2, 3, 6, 7, 4, 9}, {5, 9, 
1, 4, 6, 8, 2, 10, 7, 3}, {8, 6, 1, 9, 3, 10, 5, 2, 4, 7}, {5, 8, 1,
7, 4, 8, 9, 3, 6, 2}, {9, 8, 2, 10, 5, 1, 7, 6, 3, 4}, {1, 10, 2, 
5, 7, 4, 6, 9, 8, 3}, {1, 7, 4, 6, 8, 10, 2, 5, 3, 9}}

Using l1[[All, #, 2]] & /@ Range[1, Length[l1[[All;2]]]] seems particularily inelegant... I'm sure there's a better way.

Each sublist being the second element of each column of the l1 matrix.

$\endgroup$
  • 3
    $\begingroup$ Transpose[l1, {3, 2, 1}][[2]] $\endgroup$ – Marius Ladegård Meyer Nov 17 '16 at 14:37
  • 1
    $\begingroup$ Transpose@Map[Last, l1, {2}] $\endgroup$ – corey979 Nov 17 '16 at 15:10
  • $\begingroup$ I would like to understand, why {3,2,1} in Marius' answer? $\endgroup$ – Jerome Ibanes Nov 17 '16 at 16:10
  • 4
    $\begingroup$ Transpose@l1[[;; , ;; , 2]] $\endgroup$ – N.J.Evans Nov 17 '16 at 17:05
  • $\begingroup$ The section labelled Iteration Order in the answer (14810) discusses index gymnastics of the kind specified by {3,2,1}. That answer concerns Flatten, but it might still be useful if one can make a mental substitution according to the identity Transpose[m, {i, j, k}] === Flatten[m, {{i}, {j}, {k}}]. $\endgroup$ – WReach Nov 18 '16 at 0:35
2
$\begingroup$

This can be achieved in a number of ways:

  • by replacing the pairs with their second elements:

l1 /. {a_, b_} -> b // Transpose

  • or by mapping Last onto l1 on level 2:

Transpose @ Map[Last, l1, {2}]

  • or by explicitly taking the second elements:

Transpose @ l1[[;; , ;; , 2]] (* credit: N.J.Evans *)

  • or with a clever use of Transpose:

Transpose[l1, {3, 2, 1}][[2]] (* credit: Marius Ladegård Meyer *)


Or (unnecessarily overcomplicated, though):

Transpose @ ArrayReshape[Cases[l1, {a_, b_} :> b, 2], Drop[Dimensions[l1], -1]]
$\endgroup$
1
$\begingroup$
♯1 = #2 & @@@ # & /@ # &;

Mathematica graphics

♯1@l1

Mathematica graphics

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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