Is there a multi-dimensional version of arange/linspace in numpy?

前端 未结 9 1495
有刺的猬
有刺的猬 2020-12-02 08:31

I would like a list of 2d NumPy arrays (x,y) , where each x is in {-5, -4.5, -4, -3.5, ..., 3.5, 4, 4.5, 5} and the same for y.

I could do

x = np.ar         


        
9条回答
  •  借酒劲吻你
    2020-12-02 09:04

    I think you want np.meshgrid:

    Return coordinate matrices from coordinate vectors.

    Make N-D coordinate arrays for vectorized evaluations of N-D scalar/vector fields over N-D grids, given one-dimensional coordinate arrays x1, x2,..., xn.

    import numpy as np
    x = np.arange(-5, 5.1, 0.5)
    y = np.arange(-5, 5.1, 0.5)
    X,Y = np.meshgrid(x,y)
    

    you can convert that to your desired output with

    XY=np.array([X.flatten(),Y.flatten()]).T
    
    print XY
    array([[-5. , -5. ],
           [-4.5, -5. ],
           [-4. , -5. ],
           [-3.5, -5. ],
           [-3. , -5. ],
           [-2.5, -5. ],
           ....
           [ 3. ,  5. ],
           [ 3.5,  5. ],
           [ 4. ,  5. ],
           [ 4.5,  5. ],
           [ 5. ,  5. ]])
    

提交回复
热议问题