Suppose I have three arbitrary 1D arrays, for example:
x_p = np.array((1.0, 2.0, 3.0, 4.0, 5.0))
y_p = np.array((2.0, 3.0, 4.0))
z_p = np.array((8.0, 9.0))
These three arrays represent sampling intervals in a 3D grid, and I want to construct a 1D array of three-dimensional vectors for all intersections, something like
points = np.array([[1.0, 2.0, 8.0],
[1.0, 2.0, 9.0],
[1.0, 3.0, 8.0],
...
[5.0, 4.0, 9.0]])
Order doesn't actually matter for this. The obvious way to generate them:
npoints = len(x_p) * len(y_p) * len(z_p)
points = np.zeros((npoints, 3))
i = 0
for x in x_p:
for y in y_p:
for z in z_p:
points[i, :] = (x, y, z)
i += 1
So the question is... is there a faster way? I have looked but not found (possibly just failed to find the right Google keywords).
I am currently using this:
npoints = len(x_p) * len(y_p) * len(z_p)
points = np.zeros((npoints, 3))
i = 0
nz = len(z_p)
for x in x_p:
for y in y_p:
points[i:i+nz, 0] = x
points[i:i+nz, 1] = y
points[i:i+nz, 2] = z_p
i += nz
but I feel like I am missing some clever fancy Numpy way?
See Question&Answers more detail:
os