Finding Nonzero Elements in a Sparse Matrix
deLenn
ynaddaf at riptown.com
Tue Nov 7 17:55:51 EST 2006
Thanks for the reply.
'nonzero' deos not seem to work with sparse matrices. here is an
example:
from scipy import *
A = sparse.lil_matrix((3,3))
A[1,2] = 10
A[2,0] = -10
nonzero(A)
>>> ()
(I tried it with an ordinary matrix, and it works fine)
Cheers.
Nick Vatamaniuc wrote:
> The function you might want is nonzero() or flatnonzero()
>
> >>> from numpy import *
>
> >>> a=array([ [1,2],[0,4] ])
>
> >>> a
> array([[1, 2],
> [0, 4]])
>
> >>> flatnonzero(a)
> array([0, 1, 3])
>
> nonzero() will return the a sequence of index arrays of non zero
> elements
> flatnonzero() returns the non-zero elements of the flattened version
> of the array.
>
> Cheers,
> Nick Vatamaniuc
>
>
>
> deLenn wrote:
> > Hi,
> >
> > Does scipy have an equivalent to Matlab's 'find' function, to list the
> > indices of all nonzero elements in a sparse matrix?
> >
> > Cheers.
More information about the Python-list
mailing list