5

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?

mrz
  • 11,686
  • 2
  • 25
  • 81

5 Answers5

7

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

ArrayFlatten[data ~Reverse~ 2]

Related: Prepend 0 to sublists

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
  • That is very nice! – user1066 Oct 13 '17 at 11:45
  • I like that ... thanks a lot also for the link to your answer. – mrz Oct 13 '17 at 13:27
  • Off-topic question:Are you also the "Mr. Wizard" on the Outdoors SE forum here? – Daniel Lichtblau Oct 15 '17 at 20:35
  • @J.M. Okay, good to know. I had an unfortunate need for the sort of info at that link yesterday. – Daniel Lichtblau Oct 16 '17 at 03:32
  • @Daniel Yup, it's me. I'm no outdoorsman but I enjoy nature. – Mr.Wizard Oct 16 '17 at 14:35
  • Glad to hear. I am experimenting with a sun still in my back yard right now. Suffice it to say that it is the sort of thing I wish I had known about a couple of days ago. – Daniel Lichtblau Oct 16 '17 at 16:11
  • @DanielLichtblau Yikes! I was hoping your need was merely to dissuade an acquaintance from drinking the yellow stuff "for health." (No I'm not making that up, some people actually do this...) I guess you're OK now? – Mr.Wizard Oct 16 '17 at 16:18
  • Nothing like that alas (I've also read people do that... bad idea). I am fine now, thanks. I got caught in a bad back-country low-water situation Friday evening/Saturday morning. Mostly my own stupidity (compounded by a drought in a less-than-familiar region). Kept the overnight "discharge" for emergency usage if needed. Would describe it as roughly equal parts coffee, tea, and ghastly. Not yet sure how I feel about outing myself as an idiot in public, so I might delete this in a while. Or not. – Daniel Lichtblau Oct 16 '17 at 17:26
6
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}}

kglr
  • 394,356
  • 18
  • 477
  • 896
5
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
user1066
  • 17,923
  • 3
  • 31
  • 49
3

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}}}
J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
1
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}}
eldo
  • 67,911
  • 5
  • 60
  • 168