# [Numpy-discussion] arrray/matrix nonzero() type

Nicolas Rougier Nicolas.Rougier@loria...
Wed May 27 14:26:08 CDT 2009

```Hi again,

I  have a problem with the nonzero() function for matrix.

The following test program:

import numpy, scipy.sparse

Z = numpy.zeros((10,10))
Z[0,0] = Z[1,1] = 1

i = Z.nonzero()
print i
Zc = scipy.sparse.coo_matrix((Z[i],i))

Z = numpy.matrix(Z)
i = Z.nonzero()
print i
Zc = scipy.sparse.coo_matrix((Z[i],i))

gives me:

(array([0, 1]), array([0, 1]))
(matrix([[0, 1]]), matrix([[0, 1]]))
Traceback (most recent call last):
File "test.py", line 13, in <module>
Zc = scipy.sparse.coo_matrix((Z[i],i))
File "/Volumes/Data/Local/lib/python2.6/site-packages/scipy/sparse/
coo.py", line 179, in __init__
self._check()
File "/Volumes/Data/Local/lib/python2.6/site-packages/scipy/sparse/
coo.py", line 194, in _check
nnz = self.nnz
File "/Volumes/Data/Local/lib/python2.6/site-packages/scipy/sparse/
coo.py", line 187, in getnnz
raise ValueError('row, column, and data arrays must have rank 1')
ValueError: row, column, and data arrays must have rank 1

Is that the intended behavior ? How can I use nonzero with matrix to
build the coo one ?

Nicolas
```