I am doing the following:
SeedRandom[1];
n = 3;
data = {RandomInteger[10], RandomInteger[10, {4, 5}]} & /@ Range[n]
{{1, {{4, 0, 7, 0, 0}, {8, 6, 0, 4, 1}, {8, 5, 1, 1, 1}, {3, 2, 10, 1, 6}}},
{0, {{2, 6, 4, 5, 4}, {3, 0, 1, 3, 5}, {3, 0, 3, 2, 3}, {9, 5, 1, 5, 2}}},
{3, {{9, 1, 0, 4, 4}, {1, 5, 2, 7, 9}, {9, 8, 10, 0, 10}, {10, 7, 4, 9, 2}}}}
The result should be:
Flatten[Table[Append[#, data[[i, 1]]] & /@ data[[i, 2]], {i, n}], 1]
{{4, 0, 7, 0, 0, 1}, {8, 6, 0, 4, 1, 1}, {8, 5, 1, 1, 1, 1}, {3, 2, 10, 1, 6, 1},
{2, 6, 4, 5, 4, 0}, {3, 0, 1, 3, 5, 0}, {3, 0, 3, 2, 3, 0}, {9, 5, 1, 5, 2, 0},
{9, 1, 0, 4, 4, 3}, {1, 5, 2, 7, 9, 3}, {9, 8, 10, 0, 10, 3}, {10, 7, 4, 9, 2, 3}}
Can you propose another FAST solution instead of using Table
?
Answer
Append @@@ Join @@ (Thread /@ Reverse /@ #) & @ data
{{4, 0, 7, 0, 0, 1}, {8, 6, 0, 4, 1, 1}, {8, 5, 1, 1, 1, 1}, {3, 2, 0, 1, 6, 1}, {2, 6, 4, 5, 4, 0},
{3, 0, 1, 3, 5, 0}, {3, 0, 3, 2, 3, 0}, {9, 5, 1, 5, 2, 0}, {9, 1, 0, 4, 4, 3},
{1, 5, 2, 7, 9, 3}, {9, 8, 10, 0, 10, 3}, {10, 7, 4, 9, 2, 3}}
Comments
Post a Comment