且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

独立滚动矩阵行

更新时间:2023-12-01 12:32:58

您当然可以使用高级索引来完成此操作,是否最快的方法可能取决于您的数组大小(如果行很大,则可能不是):

Sure you can do it using advanced indexing, whether it is the fastest way probably depends on your array size (if your rows are large it may not be):

rows, column_indices = np.ogrid[:A.shape[0], :A.shape[1]]

# Use always a negative shift, so that column_indices are valid.
# (could also use module operation)
r[r < 0] += A.shape[1]
column_indices = column_indices - r[:, np.newaxis]

result = A[rows, column_indices]