Reputation: 6651
I have a 3D curvilinear grid described with coordinates x, y, z. x, y, and z are all 3D numpy arrays. For the sake of an example we can say they all have shape (20,9,10). So the size of each is 20*9*10=1800. I need to produce a 3x1800 array (or 1800x3, whatever), that stores the coordinate like so:
[ [x[0,0,0],y[0,0,0],z[0,0,0]],[x[0,0,1],y[0,0,1],z[0,0,1]], ....
[x[19,8,9],y[19,8,9],z[19,8,9]]
I accomplished this like so:
coordlist=np.zeros((1800,3))
pt = 0
>>> for k in range(x.shape[0]):
... for j in range(x.shape[1]):
... for i in range(x.shape[2]):
... coordlist[pt]=np.array((x[k,j,i],y[k,j,i],z[k,j,i]))
... pt += 1
This works, but this is numpy we're talking about here so I think there must be some much nicer loop-free way of doing this. Can anyone tell me what that might be?
Upvotes: 0
Views: 57
Reputation: 17847
If you want 1800 x 3:
coordlist = np.column_stack((x.ravel(), y.ravel(), z.ravel()))
Otherwise, use np.row_stack
to get 3 x 1800.
Upvotes: 2