这是提出的问题here的延伸(引自下面)
I have a matrix (2d numpy ndarray, to be precise):
A = np.array([[4, 0, 0], [1, 2, 3], [0, 0, 5]])
And I want to roll each row of A independently, according to roll values in another array:
r = np.array([2, 0, -1])
That is, I want to do this:
print np.array([np.roll(row, x) for row,x in zip(A, r)]) [[0 0 4] [1 2 3] [0 5 0]]
Is there a way to do this efficiently? Perhaps using fancy indexing tricks?
公认的解决方案是:
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]
基本上我想做同样的事情,除了当一个索引“超过”行的末尾时,我希望行的另一端用NaN填充,而不是值以周期性的方式移动到行的“前面”。在
也许用np.pad
的方法?但我不知道如何让它以不同的数量填充不同的行。在
受Roll rows of a matrix independently's solution的启发,这里有一个基于^{} -
样本运行-
^{pr2}$我可以把它和线性索引一起破解…它得到了正确的结果,但在大型数组上执行得相当慢。在
给出预期结果:
^{pr2}$相关问题 更多 >
编程相关推荐