Tell me more ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have two lists:

u = {{1, 3}, {2, 6}, {3, 9}}
v = {0, 4}

and I want to obtain this list from them:

z = {{{0, 1}, {4, 3}}, {{0, 2}, {4, 6}}, {{0, 3},{4, 9}}}

I guess the solution will make use of Map, Thread, or even MapThread but I've tried every combinaison I can think of with no luck. How can I do it?

share|improve this question
You could do something like Transpose[Thread /@ Thread[{v, Transpose[u]}]] – Leonid Shifrin Jan 7 at 17:37
Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the FAQs! 3) When you see good Q&A, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. ALSO, remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign` – Vitaliy Kaurov Jan 8 at 13:38

6 Answers

up vote 5 down vote accepted

This works:

Transpose[{v, #}] & /@ u
{{{0, 1}, {4, 3}}, {{0, 2}, {4, 6}}, {{0, 3}, {4, 9}}}
share|improve this answer
Perfect thanks I didn't think about Transpose – su1 Jan 8 at 8:44

Just to be different:

Thread /@ ArrayFlatten @ {{v, List /@ u}}

Thread /@ Block[{v}, Thread @ {v, u}]
share|improve this answer
Transpose /@ Tuples[{{v}, u}]
Transpose @@@ Table[{j, i}, {i, u}, {j, {v}}]
Transpose /@ Partition[Riffle[u, {v}, {1, -2, 2}], 2]

(*{{{0, 1}, {4, 3}}, {{0, 2}, {4, 6}}, {{0, 3}, {4, 9}}}*)
share|improve this answer
+1 for Tuples – Mr.Wizard Jan 8 at 6:46
Inner[List, v, #, List] & /@ u

(*  {{{0, 1}, {4, 3}}, {{0, 2}, {4, 6}}, {{0, 3}, {4, 9}}}  *)
share|improve this answer

Another possibility using Outer:

Outer[Composition[Transpose, List], {v}, u, 1][[1]]

(* {{{0, 1}, {4, 3}}, {{0, 2}, {4, 6}}, {{0, 3}, {4, 9}}} *)
share|improve this answer

Still another way, using rule-based expression rewriting.

u /. {i_, j_} -> {{v[[1]], i}, {v[[2]]}, j}

{{{0, 1}, {4, 3}}, {{0, 2}, {4, 6}}, {{0, 3}, {4, 9}}}

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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