3

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.
Added:

duplicates

user64494
  • 26,149
  • 4
  • 27
  • 56
garej
  • 4,865
  • 2
  • 19
  • 42

7 Answers7

4

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}}}
Purboo
  • 677
  • 3
  • 15
2
a = {{x1, y1}, {x2, y2}, {x3, y3}};
b = {{u1, v1}, {u2, v2}, {u3, v3}};
c = {{{x1, u1}, {y1, v1}}, {{x2, u2}, {y2, v2}}, {{x3, u3}, {y3, v3}}}

res = Thread[{a, b}] // Transpose[#, {1, 3, 2}] &

MatrixForm /@ {a, b, c, res}

enter image description here

Syed
  • 52,495
  • 4
  • 30
  • 85
2
a = {{x1, y1}, {x2, y2}, {x3, y3}};
b = {{u1, v1}, {u2, v2}, {u3, v3}};

Transpose /@ Transpose[{a, b}]

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

eldo
  • 67,911
  • 5
  • 60
  • 168
2

As of Version 13.1+, we could use Threaded like so:

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

combine = Function[{e1, e2}, {e1, e2}, Listable];

a ~ combine ~ Threaded @ b

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

But in this case it isn’t even needed as we are operating at the lowest level for two lists having the same dimensions:

a ~ combine ~ b
gwr
  • 13,452
  • 2
  • 47
  • 78
2
Transpose[{a, b}, {3, 1, 2}]

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

Karl
  • 941
  • 1
  • 7
1
a = {{x1, y1}, {x2, y2}, {x3, y3}};
b = {{u1, v1}, {u2, v2}, {u3, v3}};

Using Riffle, Partition and Thread:

Thread /@ Partition[Riffle[a, b], 2]

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

Or using Table, Partition and Riffle:

Table[Partition[Riffle[a[[i]], b[[i]]], {2}], {i, #}] &@ Max@Dimensions@{a, b}

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

E. Chan-López
  • 23,117
  • 3
  • 21
  • 44
1
Map[Variables,Partition[Flatten[a]*Flatten[b], 1]]

outputs

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

The Documentation Center is a great resource.

ben
  • 97
  • 4
  • 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 05:53