Wrap slice around edges of a 2D array in numpy

后端 未结 5 886
别跟我提以往
别跟我提以往 2020-12-15 12:00

Suppose I am working with numpy in Python and I have a two-dimensional array of arbitrary size. For convenience, let\'s say I have a 5 x 5 array. The specific numbers are n

5条回答
  •  悲&欢浪女
    2020-12-15 12:38

    You can also use roll, to roll the array and then take your slice:

    b = np.roll(np.roll(a, 1, axis=0), 1, axis=1)[:3,:3]
    

    gives

    array([[24, 20, 21],
           [ 4,  0,  1],
           [ 9,  5,  6]])
    

提交回复
热议问题