4
$\begingroup$

I have a table of dimension $M \times N \times Q$ of 3-tuples {x,y,z} that represent points, and another table of dimension $M \times N \times Q$ of scalar values. I am trying to join the two tables together to be of the form {{x,y,z}',n'} where {x,y,z}' and n' are the elements of each table at any given index.

Here is what I have so far, which generates the table of tuples and the table of scalar values

ot = ArrayReshape[Range[192], {4, 6, 8}];
x = Array[# &, 4, {-14, -7}];
y = Array[# &, 6, {225, 230}];
z = Array[# &, 8, {980, 1111}];
tup = ArrayReshape[Tuples[{z, y, x}], {4, 6, 8, 3}];

However, using Join[tup, ot] does not do the joining of the tables as I would expect, where ot and tup are joined elementwise.

$\endgroup$
  • 5
    $\begingroup$ Try Transpose[{tup, ot}, {4, 3, 2, 1}] or Flatten[{tup, ot}, {{4}, {3}, {2}, {1}}]. $\endgroup$ – J. M. will be back soon Mar 23 '16 at 15:06
  • $\begingroup$ This worked exactly how I needed, thanks! $\endgroup$ – QtizedQ Mar 23 '16 at 19:32
  • $\begingroup$ Possible duplicate of Elementwise join $\endgroup$ – garej Mar 23 '16 at 20:19
  • $\begingroup$ Related 31238 $\endgroup$ – garej Mar 23 '16 at 20:24
6
$\begingroup$

J. M.'s suggestions should work fine, but a third option is MapThread, which I find a bit more intuitive and where the level specification is a bit less awkward:

MapThread[List, {tup, ot}, 3]

Without the third parameter, MapThread is used to apply a function to all pairs of elements at corresponding positions in two (or more) lists. With the third parameter, we push that process down to the third level, such that the function is applied to all pairs with the same (m,n,q) index, i.e. the corresponding vectors and scalars. The function we apply is simply List, which wraps both of them in a list.

$\endgroup$

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