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

前端 未结 9 1498
有刺的猬
有刺的猬 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:15

    I still did it with Linspace because I prefer to stick to this command.

    You can create like the following format: np.linspace(np.zeros(width)[0], np.full((1,width),-1)[0], height)

    np.linspace(np.zeros(5)[0],np.full((1,5),-1)[0],5)
    

    Output the following:

    array([[ 0.  ,  0.  ,  0.  ,  0.  ,  0.  ],
           [-0.25, -0.25, -0.25, -0.25, -0.25],
           [-0.5 , -0.5 , -0.5 , -0.5 , -0.5 ],
           [-0.75, -0.75, -0.75, -0.75, -0.75],
           [-1.  , -1.  , -1.  , -1.  , -1.  ]])
    

    Add .tranpose() then you get:

    array([[ 0.  , -0.25, -0.5 , -0.75, -1.  ],
          [ 0.  , -0.25, -0.5 , -0.75, -1.  ],
          [ 0.  , -0.25, -0.5 , -0.75, -1.  ],
          [ 0.  , -0.25, -0.5 , -0.75, -1.  ],
          [ 0.  , -0.25, -0.5 , -0.75, -1.  ]])
    

提交回复
热议问题