相关疑难解决方法(0)

独立滚动矩阵的行

我有一个矩阵(准确地说是2d numpy ndarray):

A = np.array([[4, 0, 0],
              [1, 2, 3],
              [0, 0, 5]])
Run Code Online (Sandbox Code Playgroud)

我想A根据另一个数组中的滚动值独立滚动每一行:

r = np.array([2, 0, -1])
Run Code Online (Sandbox Code Playgroud)

也就是说,我想这样做:

print np.array([np.roll(row, x) for row,x in zip(A, r)])

[[0 0 4]
 [1 2 3]
 [0 5 0]]
Run Code Online (Sandbox Code Playgroud)

有没有办法有效地做到这一点?也许使用花哨的索引技巧?

python performance numpy

15
推荐指数
3
解决办法
2575
查看次数

标签 统计

numpy ×1

performance ×1

python ×1