2

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!

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
Histoscienology
  • 368
  • 1
  • 8

2 Answers2

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

kglr
  • 394,356
  • 18
  • 477
  • 896
0
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}}]
Coolwater
  • 20,257
  • 3
  • 35
  • 64