4
$\begingroup$

I have a data set with two dimensions as:

x,y={{{x11,y11}{x12,y12}{x13,y13}},{{x21,y21},{x22,y22},{x23,y23}},{{x31,y31},{x32,y32},{x33,y33}}}={{{1, 2}, {3, 4}, {5, 4}}, {{3, 6}, {4, 2}, {3, 7}}, {{2, 6}, {6, 
   2}, {7, 8}}}

How can I separate it to x and y as follows?

x = {{x11,x12,x13},{x21,x22,x23},{{x31,x32,x33}}={{1, 3, 5}, {3, 4, 3}, {2, 6, 7}}
y ={{y11,y12,y13},{y21,y22,y23},{{y31,y32,y33}}= {{2, 4, 4}, {6, 2, 7}, {6, 2, 8}}
$\endgroup$
1
  • $\begingroup$ See the Part documentation. If xy={{{x11,y11},{x12,y12},{x13,y13}},{{x21,y21},{x22,y22},{x23,y23}},{{x31,y31},{x32,y32},{x33,y33}}} then xy[[All,All,1]] gives {{x11,x12,x13},{x21,x22,x23},{x31,x32,x33}} and xy[[All,All,2]] gives {{y11,y12,y13},{y21,y22,y23},{y31,y32,y33}}. $\endgroup$
    – user293787
    Aug 17 at 11:13

2 Answers 2

6
$\begingroup$

Here we use two Transpose,but I think we can only use one Flatten.

list1 = {{{x11, y11}, {x12, y12}, {x13, y13}}, {{x21, y21}, {x22, 
     y22}, {x23, y23}}, {{x31, y31}, {x32, y32}, {x33, y33}}};
list2 = {{{1, 2}, {3, 4}, {5, 4}}, {{3, 6}, {4, 2}, {3, 7}}, {{2, 
     6}, {6, 2}, {7, 8}}};
Transpose[Transpose /@ list1]
Transpose[Transpose /@ list2]

enter image description here

$\endgroup$
7
  • $\begingroup$ good,how i separate it to two datasets, i think we can know the length and use iteration for sepating it $\endgroup$
    – Ahmed
    Aug 17 at 11:46
  • $\begingroup$ @Willymith Transpose[Transpose /@ list1]//First and Transpose[Transpose /@ list1]//Last. $\endgroup$
    – cvgmt
    Aug 17 at 11:50
  • $\begingroup$ i want to make them as follow x ={{1, 3, 5}, {3, 4, 3}, {2, 6, 7}} y = {{2, 4, 4}, {6, 2, 7}, {6, 2, 8}} $\endgroup$
    – Ahmed
    Aug 17 at 11:55
  • $\begingroup$ @Willymith {x, y} = Transpose[Transpose /@ list2]. $\endgroup$
    – cvgmt
    Aug 17 at 11:58
  • 4
    $\begingroup$ (+1) you could also use Transpose[list1, {2, 3, 1}] or Flatten[list1, {{3}, {1}}] $\endgroup$
    – kglr
    Aug 17 at 15:42
6
$\begingroup$
Clear["Global`*"];
xylist = {{{x11, y11}, {x12, y12}, {x13, y13}}, {{x21, y21}, {x22, 
    y22}, {x23, y23}}, {{x31, y31}, {x32, y32}, {x33, y33}}}
numlist = {{{1, 2}, {3, 4}, {5, 4}}, {{3, 6}, {4, 2}, {3, 7}}, {{2, 
    6}, {6, 2}, {7, 8}}}

xy = {xylist, numlist}

MatrixForm@xy

$$\left( \begin{array}{ccc} \left( \begin{array}{cc} \text{x11} & \text{y11} \\ \text{x12} & \text{y12} \\ \text{x13} & \text{y13} \\ \end{array} \right) & \left( \begin{array}{cc} \text{x21} & \text{y21} \\ \text{x22} & \text{y22} \\ \text{x23} & \text{y23} \\ \end{array} \right) & \left( \begin{array}{cc} \text{x31} & \text{y31} \\ \text{x32} & \text{y32} \\ \text{x33} & \text{y33} \\ \end{array} \right) \\ \left( \begin{array}{cc} 1 & 2 \\ 3 & 4 \\ 5 & 4 \\ \end{array} \right) & \left( \begin{array}{cc} 3 & 6 \\ 4 & 2 \\ 3 & 7 \\ \end{array} \right) & \left( \begin{array}{cc} 2 & 6 \\ 6 & 2 \\ 7 & 8 \\ \end{array} \right) \\ \end{array} \right)$$

It is easier to visualize how to pair these columns up now.

x = Set @@ xy[[All, All, All, 1]]
y = Set @@ xy[[All, All, All, 2]]

{x, y}

{{{1, 3, 5}, {3, 4, 3}, {2, 6, 7}}, {{2, 4, 4}, {6, 2, 7}, {6, 2, 8}}}

These values have also been assigned to the corresponding variables.

$\endgroup$

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