2
$\begingroup$

This question already has an answer here:

My question is to combine a matrix of values with a matrix of 2D coordinates:

Coor = {{{a, b}, {c, d}}, {{u, v}, {s, t}}};
Value = {{1, 2}, {3, 4}};

I want to get

Combined = {{{a, b,1}, {c, d,2}}, {{u, v,3}, {s, t,4}}};

so I can use ListPlot3D

This is similar to another question here. I attempt to use this solution by doing this

MapThread[List, {matrix1, matrix}, 2]

But this gives me {{{{a, b},1}, {{c, d},2}}...}; which has an extra bracket inside that I couldn't figure out how to remove...

How can I get the desired "Combined" list? Thanks!

$\endgroup$

marked as duplicate by Kuba list-manipulation Aug 6 at 10:27

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3
$\begingroup$
Coor = {{{a, b}, {c, d}}, {{u, v}, {s, t}}};
Val = {{1, 2}, {3, 4}};

MapThread[Append, {Coor, Val}, 2]

{{{a, b, 1}, {c, d, 2}}, {{u, v, 3}, {s, t, 4}}}

Also

Join[Coor, Map[List, Val, {-1}] , 3]

{{{a, b, 1}, {c, d, 2}}, {{u, v, 3}, {s, t, 4}}}

$\endgroup$
0
$\begingroup$
coor = {{{a, b}, {c, d}}, {{u, v}, {s, t}}};
val = {{1, 2}, {3, 4}};

Join[coor, Transpose[{val}, {3, 1, 2}], 3]
(* {{{a, b, 1}, {c, d, 2}}, {{u, v, 3}, {s, t, 4}}} *)

Or alternatively:

Flatten[{Transpose[coor, {2, 3, 1}], {val}}, {{3}, {4}, {1, 2}}]
$\endgroup$

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