wrapping around slices in Python / numpy

后端 未结 7 2241
耶瑟儿~
耶瑟儿~ 2020-12-05 17:41

I have a numpy array, and I want to get the \"neighbourhood\" of the i\'th point. Usually the arrays I\'m using are two-dimensional, but the following 1D example illustrates

7条回答
  •  隐瞒了意图╮
    2020-12-05 18:16

    If you don't have the convenience of using np.take with mode='wrap' (e.g. when using numba), the following works just the same:

    A = numpy.array([0,10,20,30,40,50,60,70,80,90])
    indices = range(i-2, i+3)
    neighbourhood = A.take(indices % len(A))
    

    or

    A = numpy.array([0,10,20,30,40,50,60,70,80,90])
    indices = range(i-2, i+3)
    neighbourhood = A[indices % len(A)]
    

提交回复
热议问题