【发布时间】:2015-02-05 12:24:09
【问题描述】:
我在 scipy 中有一个 256x256 csr_matrix,我有一个我想应用的新行顺序列表。我试过这个:
def HblockDiag(z):
Hz = H(z) # H(z) returns a 256x256 csr_matrix
Hz.indices = idenRows
return Hz
但它不起作用,因为indices 没有给出每一行的索引......最好的方法是什么?
编辑:
def HblockDiag(H, idenRows):
x = H.tocoo()
idenRows = np.asarray(idenRows, dtype=x.row.dtype)
x.row = idenRows[x.row]
H = x.tocsr()
return H
test = sps.csr_matrix([[1,2,4],[6,3,4],[8,5,2]])
print test.toarray()
test = HblockDiag(test, [2,0,1])
print test.toarray()
我明白了:
[[1 2 4]
[6 3 4]
[8 5 2]]
[[6 3 4]
[8 5 2]
[1 2 4]]
相反,我想得到:
[[8 5 2]
[1 2 4]
[6 3 4]]
【问题讨论】:
-
什么是
idenRows?除非你了解csr格式,否则最好使用索引,而不是尝试直接修改属性。 -
idenRows是具有新行顺序的向量。所以:[0,1,3,2] 将交换第 2 行和第 3 行。(这只是一个例子,idenRows实际上是 256 个元素长) -
只是为了确定:想要的结果是
[[8 5 2], [1 2 4], [6 3 4]]吗?如果是这样,请编辑问题以包含此内容。