Skip to content

Instantly share code, notes, and snippets.

@vtraag
Last active December 18, 2024 22:33
Show Gist options
  • Save vtraag/8b82e10e57d93eacc524 to your computer and use it in GitHub Desktop.
Save vtraag/8b82e10e57d93eacc524 to your computer and use it in GitHub Desktop.
Permute sparse matrix in Python using scipy COO format
def permute_sparse_matrix(M, order):
permuted_row = order[M.row];
permuted_col = order[M.col];
new_M = sparse.coo_matrix((M.data, (permuted_row, permuted_col)), shape=M.shape);
return new_M;
@cesare-corrado
Copy link

It does not work; the matrix is not ordered as supposed to

@liruipeng
Copy link

The code looks fine to me. order is the permutation array that defines the old-to-new reindexing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment