Creating a numpy array of 3D coordinates from three 1D arrays

前端 未结 4 702
名媛妹妹
名媛妹妹 2020-12-03 14:15

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))
<         


        
4条回答
  •  萌比男神i
    2020-12-03 14:56

    For those who had to stay with numpy <1.7.x, here is a simple two-liner solution:

    g_p=np.zeros((x_p.size, y_p.size, z_p.size))
    array_you_want=array(zip(*[item.flatten() for item in \
                                     [g_p+x_p[...,np.newaxis,np.newaxis],\
                                      g_p+y_p[...,np.newaxis],\
                                      g_p+z_p]]))
    

    Very easy to expand to even higher dimenision as well. Cheers!

提交回复
热议问题