How can I create a slice object for Numpy array?

后端 未结 2 2014
梦毁少年i
梦毁少年i 2021-01-07 19:30

I\'ve tried to find a neat solution to this, but I\'m slicing several 2D arrays of the same shape in the same manner. I\'ve tidied it up as much as I can by defining a list

2条回答
  •  不要未来只要你来
    2021-01-07 20:17

    Yes you can use numpy.s_:

    Example:

    >>> a = np.arange(10).reshape(2, 5)
    >>> 
    >>> m = np.s_[0:2, 3:4]
    >>> 
    >>> a[m]
    array([[3],
           [8]])
    

    And in this case:

    my_slice = np.s_[cpix[1]-50:cpix[1]+50, cpix[0]-50:cpix[0]+50]
    
    a1 = array1[my_slice] 
    a2 = array2[my_slice] 
    a3 = array3[my_slice]
    

    You can also use numpy.r_ in order to translates slice objects to concatenation along the first axis.

提交回复
热议问题