0

Mathematica newbie here, thanks for your patience. I have a list of 2-element lists, i.e.

{{x1,y1}, {x2,y2}, ... {xn,yn}}

But I want to rearrange them so that they are two lists of N elements, i.e.

{{x1,x2, ... xn},{y1,y2, ... yn}}

I'm sure this is child's play for most of you, but it's really giving me trouble. I found another page that I thought would be a solution: Pretty way to group elements at odd and even positions

The answers there take a flat list {x1,y1,x2,y2, ... xn,yn} and produce the output that I want. But I need to flatten my list first. But when I call Flatten inside of one of those functions it doesn't work. The authors of the solutions have used MMA magic which I do not yet comprehend. So here are some examples of my modifications of the solutions from the page linked above which do not work:

unzip[listoftuples_] :=
 Take[Flatten[listoftuples], {#, -1, 2}] & /@ {1, 2}

unzip[listoftuples_] := Flatten[listoftuples][[# ;; ;; 2]] & /@ {1, 2}

unzip[listoftuples_] := Downsample[Flatten[listoftuples], 2, #] & /@ {1, 2}

Yet if I remove Flatten[] from any of those solutions and instead call Flatten[] outside of that function, i.e. unzip[Flatten[data]] then it works. MMA is obviously powerful and flexible, but really confusing and cryptic to newcomers.

Michael E2
  • 235,386
  • 17
  • 334
  • 747
Scott
  • 31
  • 1
  • 5
    This is an ideal job for Transpose. – CA Trevillian Jul 22 '22 at 22:16
  • Perfect, thanks for the simple solution! But neither of you can get credit for it since you didn't officially answer it. – Scott Jul 22 '22 at 22:29
  • Scott, I see the questions at the end of your post might be more interesting to answer (& I do not know with confidence what the answer is). Perhaps you can edit your question to match those (more interesting IMO) questions? – CA Trevillian Jul 22 '22 at 22:41
  • You can format inline code and code blocks by selecting the code and clicking the {} button above the edit window. The edit window help button ? is useful for learning how to format your questions and answers. You may also find the meta Q&A, How to copy code from Mathematica so it looks good on this site, helpful – Michael E2 Jul 22 '22 at 23:08
  • 4
    I'm not sure I understand the last paragraph. I did this and got the same results: https://i.stack.imgur.com/zyP4Y.png — You should make sure you start a function definition with something like ClearAll[unzip] to be sure you are starting over. Symbols in Mma are allowed to have multiple definitions. It something that new users don't always appreciate (e.g. that a typo persists and causes a bug). – Michael E2 Jul 22 '22 at 23:14

0 Answers0