It seems the most efficient is a pure Inner
approach unlike in the other answer where it is used with Map
.
Inner[ {#1, #2} &, v, Transpose @ u, List]
or simply
Inner[List, v, Transpose @ u, List]
{{{0, 1}, {4, 3}}, {{0, 2}, {4, 6}}, {{0, 3}, {4, 9}}}
Edit
I tested this solution with tag
instead of v
, where
tag = {a, b, c, d};
In the following we compare efficiency of provided resonable solutions.
Let's define:
rmrf1[tag_, test_] := First@AbsoluteTiming[ Riffle[ tag, #] ~ Partition ~ 2 & /@ test]
rmrf2[tag_, test_] := First@AbsoluteTiming[ Thread[ {tag, #}] & /@ test]
trMap[tag_, test_] := First@AbsoluteTiming[ Transpose[{tag, #}] & /@ test]
Artes2[tag_, test_] := First@AbsoluteTiming[ Inner[List, tag, Transpose @ test, List]]
MrWizChan[tag_, test_] := First@AbsoluteTiming[Transpose[Tuples@{{tag}, test}, {1, 3, 2}]]
Let's choose some sets of data:
ts1 = RandomInteger[100, {3 10^5, 4}];
ts2 = RandomInteger[100, {10^6, 4}];
now we have:
rmrf1[tag, ts1]
rmrf2[tag, ts1]
trMap[tag, ts1]
MrWizChan[tag, ts1]
Artes2[tag, ts1]
2.817000
1.386000
1.577000
1.054000
0.438000
and
rmrf1[tag, ts2]
rmrf2[tag, ts2]
trMap[tag, ts2]
MrWizChan[tag, ts2]
Artes2[tag, ts2]
9.383000
4.357000
5.051000
3.585000
1.476000
These results clearly demonstrate that the Inner
solution is the best, while the other ones (involving mapping Thread
, Riffle
, Transpose
or transposing Tuples
) are at least a few times slower. In fact, we get similar results with other data like e.g. RandomReal
.
Transpose[Thread /@ Thread[{v, Transpose[u]}]]
$\endgroup$Read the FAQs
! 3) When you see good Q&A, vote them up byclicking 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
` $\endgroup$