5
$\begingroup$

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?

$\endgroup$
2
  • 1
    $\begingroup$ Append @@@ Join @@ (Thread /@ Reverse /@ data)? $\endgroup$
    – kglr
    Commented Oct 12, 2017 at 14:44
  • $\begingroup$ @kglr: wau, please put this into an answer ... this is incredably fast $\endgroup$
    – mrz
    Commented Oct 12, 2017 at 14:55

5 Answers 5

6
$\begingroup$
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}}

$\endgroup$
1
  • $\begingroup$ thanks a lot for the really FAST solution. $\endgroup$
    – mrz
    Commented Oct 13, 2017 at 13:31
7
$\begingroup$

tomd was close (+1) but there is better:

ArrayFlatten[data ~Reverse~ 2]

Related: Prepend 0 to sublists

$\endgroup$
9
  • $\begingroup$ That is very nice! $\endgroup$
    – user1066
    Commented Oct 13, 2017 at 11:45
  • $\begingroup$ I like that ... thanks a lot also for the link to your answer. $\endgroup$
    – mrz
    Commented Oct 13, 2017 at 13:27
  • $\begingroup$ Off-topic question:Are you also the "Mr. Wizard" on the Outdoors SE forum here? $\endgroup$ Commented Oct 15, 2017 at 20:35
  • $\begingroup$ @Daniel, yes, it's him. $\endgroup$ Commented Oct 16, 2017 at 3:26
  • $\begingroup$ @J.M. Okay, good to know. I had an unfortunate need for the sort of info at that link yesterday. $\endgroup$ Commented Oct 16, 2017 at 3:32
5
$\begingroup$
ArrayFlatten[{#}] & /@ Reverse /@ data  // Catenate

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

See this answer, due to Janus, at SO.

Slight modification of above:

ArrayFlatten[{#}] & /@ Reverse[data, 2] // Catenate


Edit

Mr Wizard, in this answer, gives an elegant modification:

Reverse[data, 2] // ArrayFlatten

Just for fun:

Flatten /@ Tuples[{ #[[2]], {#[[1]]}}] & /@ data // Catenate
$\endgroup$
2
  • $\begingroup$ Thank you ... this is also very fast and elegant too $\endgroup$
    – mrz
    Commented Oct 13, 2017 at 2:02
  • 1
    $\begingroup$ ArrayFlatten is a really useful function. It's very good to try and get it into your repertoire. $\endgroup$ Commented Oct 13, 2017 at 9:30
3
$\begingroup$

Try this:

BlockRandom[SeedRandom[1]; 
            Table[With[{pad = RandomInteger[10]}, 
                       PadRight[RandomInteger[10, {4, 5}], {4, 6}, pad]], {3}]]
   {{{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}}}
$\endgroup$
2
  • $\begingroup$ thank you ... in my real data the number of elements can be anything and is not fixed (my example does not show it) $\endgroup$
    – mrz
    Commented Oct 12, 2017 at 14:58
  • 1
    $\begingroup$ Should've made a more representative example, then. $\endgroup$ Commented Oct 12, 2017 at 15:05
1
$\begingroup$
list =
  {{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}}}};

MapApply came with V 13.1

Join @@ MapApply[Append @ #1 /@ #2 &] @ list

gives

{{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}}
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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