# Combine/Transpose arrays on other dimensions

I have two arrays:

Dimensions[InputData]

{200, 200, 3}

Dimensions[OutputData]

{200, 200}

InputData[[1 ;; 2, 1 ;; 2]]

{{{0.294118, 0.372549, 0.164706}, {0.298039, 0.376471, 0.176471}},
{{0.301961, 0.392157, 0.188235}, {0.301961, 0.392157, 0.188235}}}

OutputData[[1 ;; 2, 1 ;; 2]]

{{42., 41.}, {43., 44.}}


And I would like this (all the range):

{{{0.294118, 0.372549, 0.164706, 42.}, {0.298039, 0.376471, 0.176471, 41}},
{{0.301961, 0.392157, 0.188235, 43}, {0.301961, 0.392157, 0.188235, 44}}}

• One possibility: Join[InputData, Map[List, OutputData, {-1}], 3] – Carl Woll Dec 21 '17 at 0:21
• Thanks! It worked, what does this part mean: , {-1}], 3] – pnz Dec 21 '17 at 0:26
• To use Join with level spec 3, both datasets need to have array depth 3. The Map adds another {} wrapper at the lowest level so that OutputData has the right depth. Probably Map[List, OutputData, {2}] was a bit safer. – Carl Woll Dec 21 '17 at 1:13
• Thanks, for the instructions :) – pnz Dec 21 '17 at 1:23

Contriving some reproducible data.

With[{n = 5},
SeedRandom[42];
data1 = RandomReal[1., {n, n, 3}];
data2 = N @ RandomInteger[42, {n, n}]];

data1

{{{0.425905, 0.391023, 0.347069}, {0.453741, 0.555963, 0.289169},
{0.296848, 0.206408, 0.32517}, {0.973325, 0.258796, 0.550582},
{0.717287, 0.754353, 0.860349}},
{{0.996966, 0.739226, 0.0383646}, {0.376127, 0.296199, 0.528142},
{0.607352, 0.902795, 0.840683}, {0.885752, 0.527083, 0.560605},
{0.924421, 0.00157704, 0.90062}},
{{0.367489, 0.274115, 0.97712}, {0.96073, 0.478762, 0.848851},
{0.352932, 0.799515, 0.365826}, {0.739896, 0.789536, 0.807022},
{0.534674, 0.164278, 0.362973}},
{{0.444317, 0.341289, 0.826573}, {0.51915, 0.879569, 0.676402},
{0.940305, 0.207703, 0.620014}, {0.769342, 0.747256, 0.499309},
{0.909234, 0.848706, 0.0663821}},
{{0.335239, 0.998651, 0.690789}, {0.263656, 0.389755, 0.303175},
{0.390042, 0.768988, 0.592667}, {0.688916, 0.999346, 0.658294},
{0.194409, 0.893499, 0.194503}}}

data2

{{42., 25., 3., 28., 15.}, {3., 14., 9., 35., 39.}, {29., 14., 2.,  40., 19.},
{40., 26., 10., 33., 25.}, {39., 24., 20., 25., 30.}}


Here is a simple and easy-to-understand way to do what you want:

MapThread[{Sequence @@ #1, #2} &, {data1, data2}, 2]


and, just for fun, here is a more exotic way

ArrayReshape[Riffle[Catenate[data1], Flatten[data2]], Dimensions[data1] + {0, 0, 1}]


Both methods produce

{{{0.425905, 0.391023, 0.347069, 42.}, {0.453741, 0.555963, 0.289169, 25.},
{0.296848, 0.206408, 0.32517, 3.}, {0.973325, 0.258796, 0.550582, 28.},
{0.717287, 0.754353, 0.860349, 15.}},
{{0.996966, 0.739226, 0.0383646, 3.}, {0.376127, 0.296199, 0.528142, 14.},
{0.607352, 0.902795, 0.840683, 9.}, {0.885752, 0.527083, 0.560605, 35.},
{0.924421, 0.00157704, 0.90062, 39.}},
{{0.367489, 0.274115, 0.97712, 29.}, {0.96073, 0.478762, 0.848851, 14.},
{0.352932, 0.799515, 0.365826, 2.}, {0.739896, 0.789536, 0.807022, 40.},
{0.534674, 0.164278, 0.362973, 19.}},
{{0.444317, 0.341289, 0.826573, 40.}, {0.51915, 0.879569, 0.676402, 26.},
{0.940305, 0.207703, 0.620014, 10.}, {0.769342, 0.747256, 0.499309, 33.},
{0.909234, 0.848706, 0.0663821, 25.}},
{{0.335239, 0.998651, 0.690789, 39.}, {0.263656, 0.389755, 0.303175, 24.},
{0.390042, 0.768988, 0.592667, 20.}, {0.688916, 0.999346, 0.658294, 25.},
{0.194409, 0.893499, 0.194503, 30.}}}