On 4/20/07, Dennis Cooke <[EMAIL PROTECTED]> wrote:
I'm an ex-Matlab user trying to come up to speed with python and numpy. I'm confused on how to use the Numpy functions nonzero() and where(). In matlab, the equivalent function (find(a>0) ) would return an array, whereas in numpy, where() or nonzero() will return a single element tuple. For example: In [35]: x = [ 0, 0, 0, 99, 0, 1, 5] # and I wish to file the index of the first non-zero element of x - which is 3. In [36]: from numpy import nonzero In [37]: i=nonzero(x) In [38]: i Out[38]: (array([3, 5, 6]),) In [39]: i[0] Out[39]: array([3, 5, 6]) so nonzero() has output a tuple with a single element = the string 'array([3, 5, 6])'
What makes you think that this is a string? It is in fact the array that you seek:
from numpy import nonzero x = [ 0, 0, 0, 99, 0, 1, 5] i = nonzero(x) i
(array([3, 5, 6]),)
i[0]
array([3, 5, 6])
type(i[0])
<type 'numpy.ndarray'>
i[0][0]
3 How do I convert this string (or the original tuple) into an array where
the first element = 3 (the index of the first non-zero element of my original array x).
I fairly certain that you just want i[0]. -tim _______________________________________________
Numpy-discussion mailing list [email protected] http://projects.scipy.org/mailman/listinfo/numpy-discussion
-- //=][=\\ [EMAIL PROTECTED]
_______________________________________________ Numpy-discussion mailing list [email protected] http://projects.scipy.org/mailman/listinfo/numpy-discussion
