I have a 256x256 csr_matrix in scipy and I have a list of a new row order that I'd like to apply. I tried this:
def HblockDiag(z):
Hz = H(z) # H(z) returns a 256x256 csr_matrix
Hz.indices = idenRows
return Hz
but it didn't work work because indices doesn't give the indices of each row... What's the best way to do this?
Edit:
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()
I get:
[[1 2 4]
[6 3 4]
[8 5 2]]
[[6 3 4]
[8 5 2]
[1 2 4]]
instead, I would like to get:
[[8 5 2]
[1 2 4]
[6 3 4]]
idenRows? Unless you understand thecsrformat, it is better to use indexing, rather trying to modify the attributes directly. - hpauljidenRowsis a vector with the new orde of rows. So: [0,1,3,2] would swap rows 2 and 3. (that was just an example,idenRowsis actually 256 elements long) - Jean-Luc[[8 5 2], [1 2 4], [6 3 4]]? If so, please edit the question to include this. - unutbu