Though not nearly as general as the method using Table
here is a faster method for this specific case:
Join[#.{{10}, {1}}, #, 2] & @ Tuples @ Range @ {4, 3}
{{11, 1, 1}, {12, 1, 2}, {13, 1, 3}, {21, 2, 1}, {22, 2, 2}, {23, 2, 3},
{31, 3, 1}, {32, 3, 2}, {33, 3, 3}, {41, 4, 1}, {42, 4, 2}, {43, 4, 3}}
Comparative timings:
SetAttributes[timeAvg, HoldFirst]
timeAvg[func_] :=
Do[If[# > 0.3, Return[#/5^i]] & @@ Timing@Do[func, {5^i}], {i, 0, 15}]
{x, y} = {100, 50};
{FromDigits@{##}, ##} & @@@ Tuples @ Range @ {x, y} // timeAvg
Table[{10 i + j, i, j}, {i, x}, {j, y}] ~Flatten~ 1 // timeAvg
Array[{10 # + #2, #, #2} &, {x, y}] ~Flatten~ 1 // timeAvg
Join[#.{{10}, {1}}, #, 2] & @ Tuples @ Range @ {x, y} // timeAvg
0.004248
0.002496
0.00026976
0.00017984
FromDigits
is clever but slow, Array
auto-complies and is an order of magnitude faster than Table
, and Dot
is faster still.
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$ – chris Nov 6 '12 at 20:37