# Making a table with elements like {10i+j, i, j}

Table[10i+j,{i,4},{j,3}] will generate a table that looks like this: {{11,12,13},{21,22,23},{31,32,33},{41,42,43}}

I would like to have a table with elements like {10i+j, i, j}, I mean {{11, 1, 1}, {12, 1, 2}, ..., {43, 4, 3}}

How do I make such a table?

-
Flatten[Table[{10i + j, i, j}, {i, 1, 4}, {j, 1, 3}], 1] –  ssch Nov 6 '12 at 20:28
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 –  chris Nov 6 '12 at 20:37
@ssch Please post that as an answer instead of a comment. –  Brett Champion Nov 6 '12 at 21:50

There is one way to produce the expected result with Table pointed out by ssch in the comments, here are another ones, with Array :

Array[{10 #1 + #2, #1, #2} &, {4, 3}] // Flatten[#, 1] &


or with a powerful function like Outer :

Outer[{10 #1 + #2, #1, #2} &, Range[4], Range[3]] // Flatten[#, 1] &

{{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} }

-
Shorter: Join @@ Array[{10 # + #2, #, #2} &, {4, 3}] –  Mr.Wizard Nov 21 '12 at 19:59
@Mr.Wizard Nicer but a bit slower. –  Artes Nov 22 '12 at 13:51
Join @@ Table[{10 i + j, i, j}, {i, 4}, {j, 3}]
Sequence @@@ Table[{10 i + j, i, j}, {i, 4}, {j, 3}]
Tuples[Range@{4, 3}] /. {x_, y_} -> {10 x + y, x, y}
Level[#, {2}] &@Table[{10 i + j, i, j}, {i, 4}, {j, 3}]

-

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.

-
Nice analysis, ++ . Why do you use timeAvg instead of e.g. AbsoluteTiming ? –  Artes Nov 21 '12 at 20:31
@Artes thanks. I use it for consistency as the time taken is too short to be accurate. I would have to create a huge array to get an accurate timing in one pass and I don't have enough RAM for that. –  Mr.Wizard Nov 21 '12 at 20:33
@Artes on second thought I could have used a larger array dimension but I would still favor timeAvg out of habit. –  Mr.Wizard Nov 21 '12 at 20:35
Dot is great and often underused, +1. I expected FromDigits to be slow and it clearly lived up to my expectations. –  Rojo Nov 21 '12 at 20:48

An alternative

{FromDigits@{##} , ##} & @@@ Tuples@Range@{4, 3}

-
+1 for being clever -- btw {10 # + #2, ##} & @@@ Tuples@Range@{4, 3} is shorter. –  Mr.Wizard Nov 21 '12 at 19:58
@Mr.Wizard Thanks. I prefer the longer but higher level and more extensible version –  Rojo Nov 21 '12 at 20:02

If you create a table like:

Table[{10i + j, i, j}, {i, 1, 4}, {j, 1, 3}]


that nearly gives you what you want, but you'll notice they are grouped together, but that can be sorted by simply flattening that result appropriately

Flatten[Table[{10i + j, i, j}, {i, 1, 4}, {j, 1, 3}], 1]
`
-