List Operations: how to combine two lists (pattern given)

Two lists (matrixes) are given:

a = {{x1, y1}, {x2, y2}, {x3, y3}};
b = {{u1, v1}, {u2, v2}, {u3, v3}};

Expected return:

c = {{{x1, u1}, {y1, v1}}, {{x2, u2}, {y2, v2}}, {{x3, u3}, {y3, v3}}}

Please, help to solve this.
Useful references are also welcome.





Have a look at ArrayFlatten[] or MapThread[] and Transpose[].
– J. M.♦
May 26 ’15 at 16:24


2 Answers


MapThread can solve this.

a = {{x1, y1}, {x2, y2}, {x3, y3}};
b = {{u1, v1}, {u2, v2}, {u3, v3}};
c = MapThread[List, #] & /@ MapThread[List, {a, b}]

The output is

{{{x1, u1}, {y1, v1}}, {{x2, u2}, {y2, v2}}, {{x3, u3}, {y3, v3}}}



even shorter: MapThread[List, {a, b}, 2]
– chuy
May 26 ’15 at 16:29



Even harder to read: Transpose@{#1,#2}&@@@Transpose@{a,b} 🙂
– N.J.Evans
May 26 ’15 at 16:34



My fault not to think and search carefully before asking. Nice general way: SetAttributes[add, Listable]; add[x_,y_]:={x,y}
– garej
May 26 ’15 at 19:52

Map[Variables,Partition[Flatten[a]*Flatten[b], 1]]


{{u1,x1}, {v1,y1}, {u2,x2}, {v2,y2}, {u3,x3}, {v3,y3}}

The Documentation Center is a great resource.



Thank U, I’ve noticed that DC has a direct solution to my ‘problem’. BTW, output is not exactly as expected.
– garej
May 27 ’15 at 5:53