3
votes

Here is the docstring for the MATLAB built-in function spones(S):

spones

Replace nonzero sparse matrix elements with ones. R = spones(S) generates a matrix with the same sparsity structure as S, but with ones in the nonzero positions.

I'd like to have a close equivalent of this function using numpy/scipy data structures (e.g. sparse matrices from scipy.sparse). How can I do this efficiently?

1

1 Answers

6
votes
x = ... some sparse matrix ...
y = x.copy().tocsr()
y.data.fill(1)