python numpy roll with padding

后端 未结 7 1030
孤街浪徒
孤街浪徒 2020-12-16 09:17

I\'d like to roll a 2D numpy in python, except that I\'d like pad the ends with zeros rather than roll the data as if its periodic.

Specifically, the following code<

7条回答
  •  无人及你
    2020-12-16 09:53

    I don't think that you are going to find an easier way to do this that is built-in. The touch-up seems quite simple to me:

    y = np.roll(x,1,axis=1)
    y[:,0] = 0
    

    If you want this to be more direct then maybe you could copy the roll function to a new function and change it to do what you want. The roll() function is in the site-packages\core\numeric.py file.

提交回复
热议问题