Compilation of a 2d array and a 1d array
You can use PadRight
:
PadRight[
{{x1, y1}, {x2, y2}},
{Automatic, 3},
z1
]
{{x1, y1, z1}, {x2, y2, z1}}
Or ArrayPad
:
ArrayPad[
{{x1, y1}, {x2, y2}, {x3, y3}},
{{0,0},{0,1}},
z1
]
{{x1, y1, z1}, {x2, y2, z1}, {x3, y3, z1}}
n = 1000000;
a = RandomReal[{-1, 1}, {n, 2}];
b = ConstantArray[0., 1];
c = Join[a, ConstantArray[b, Length[a]], 2]; // RepeatedTiming // First
0.016
xy = {{x1, y1}, {x2, y2}, ...}
z = {z1, z2, ...}
xyz = Partition[Flatten[Riffle[xy, z]], 3]
and you're done. Riffle
also works with just one z-value: it'll do exactly what you asked, after re-reading your question more carefully.
Explanation:
First, Riffle[list1, list2 **or** element, so just z1 works, too]
makes
{ {x1, y1}, z1, {x2, y2}, z2, ...}
then, Flatten[list]
makes
{ x1, y1, z1, x2, y2, z2, ... }
finally, Partition[list, 3]
turns it into
{ {x1, y1, z1}, {x2, y2, z2}, ...}