[SciPy-user] csr_matrix

Robert Cimrman cimrman3 at ntc.zcu.cz
Wed Nov 1 04:45:29 EST 2006


Sam frizou wrote:
> Hi,
> 
> Is it possible to get the set of indices of non null elements of a
> sparse matrix M ?
> I means I can see them by doing "print M", but I want to use them, so
> I would like something like a list of (i,j).

A general way (should work for any kind of sparse matrix) is:
[M.rowcol(ii) for ii in xrange(M.size)].

for csr_matrix, you can use directly M.colind (array of all column
indices), M.indptr (array pointing to first index of a row in colind)
attributes, but I would not recommend it, unless the first approach is
too slow.

cheers,
r.




More information about the SciPy-User mailing list