Creating a numpy array of 3D coordinates from three 1D arrays, first index changing fastest
类似于这里的问题
我有三个任意的一维数组,例如:
1 2 3 | x_p = np.array((0.0,1.1, 2.2, 3.3, 4.4)) y_p = np.array((5.5,6.6,7.7)) z_p = np.array((8.8, 9.9)) |
我需要
1 2 3 4 5 | points = np.array([[0.0, 5.5, 8.8], [1.1, 5.5, 8.8], [2.2, 5.5, 8.8], ... [4.4, 7.7, 9.9]]) |
1)第一个索引更改最快; 2)点是浮点坐标,而不是整数索引。
3)我注意到从1.7.0版开始,numpy.meshgrid更改了默认值
以获得最后一个索引快速更改的结果点,这不是我想要的。(仅从1.7.0开始提到,
我通过反复试验发现了这一点。
我认为
为了使
我使用
1 2 3 4 5 6 7 8 9 10 11 12 | In [100]: np.stack(np.meshgrid(z_p, y_p, x_p, indexing='ij'),3).reshape(-1,3)[:,::-1] Out[100]: array([[ 0. , 5.5, 8.8], [ 1.1, 5.5, 8.8], [ 2.2, 5.5, 8.8], [ 3.3, 5.5, 8.8], [ 4.4, 5.5, 8.8], [ 0. , 6.6, 8.8], ... [ 2.2, 7.7, 9.9], [ 3.3, 7.7, 9.9], [ 4.4, 7.7, 9.9]]) |
您可以用
1 | np.array(np.meshgrid(x_p, y_p, z_p)).transpose(3,1,2,0).reshape(-1,3) |
样本输出-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | In [104]: np.array(np.meshgrid(x_p, y_p, z_p)).transpose(3,1,2,0).reshape(-1,3) Out[104]: array([[ 0. , 5.5, 8.8], [ 1.1, 5.5, 8.8], [ 2.2, 5.5, 8.8], [ 3.3, 5.5, 8.8], [ 4.4, 5.5, 8.8], [ 0. , 6.6, 8.8], [ 1.1, 6.6, 8.8], [ 2.2, 6.6, 8.8], [ 3.3, 6.6, 8.8], [ 4.4, 6.6, 8.8], [ 0. , 7.7, 8.8], [ 1.1, 7.7, 8.8], .... [ 3.3, 7.7, 9.9], [ 4.4, 7.7, 9.9]]) |