circular numpy array indices

前端 未结 4 1456
既然无缘
既然无缘 2021-02-20 14:04

I have a 1-D numpy array a = [1,2,3,4,5,6] and a function that gets two inputs, starting_index and ending_index, and returns a[stari

4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-20 14:58

    An alternative that you can use is the numpy roll function combined with indexing:

    # -*- coding: utf-8 -*-
    import numpy as np
    
    def circular_array(starting_index, ending_index):
    
        idx = np.arange(1,7)
        idx = np.roll(idx, -starting_index)[:(len(idx)-starting_index+ending_index)%len(idx)]
    
        return idx
    
    
    a = circular_array(4, 1)
    print a
    

提交回复
热议问题