Re: [Numpy-discussion] counting non-zero entries in an ndarray

2010-12-22 Thread Ian Stokes-Rees
On 12/22/10 9:16 AM, Ian Stokes-Rees wrote: > What is the most efficient way to do the Matlab equivalent of nnz(M) > (nnz = number-of-non-zeros function)? Thanks to all the various responses. I should have mentioned that I'm using scipy.sparse, and lil_matrix objects have a method "getnnz()" wh

Re: [Numpy-discussion] counting non-zero entries in an ndarray

2010-12-22 Thread Jonathan Rocher
To answer the part about the most efficient way to do that, In [1]: a = array([0,1,4,76,3,0,4,67,9,5,3,9,0,5,23,3,0,5,3,3,0,5,0]) In [8]: %timeit len(where(a!=0)[0]) 10 loops, best of 3: 6.54 us per loop In [9]: %timeit (a!=0).sum() 10 loops, best of 3: 9.81 us per loop Seems like the w

Re: [Numpy-discussion] counting non-zero entries in an ndarray

2010-12-22 Thread Thomas K Gamble
On Wednesday, December 22, 2010 07:16:17 am Ian Stokes-Rees wrote: > What is the most efficient way to do the Matlab equivalent of nnz(M) > (nnz = number-of-non-zeros function)? > > I've tried Google, but no luck. > > My assumption is that something like > > a != 0 > > will be used, but I'm not

Re: [Numpy-discussion] counting non-zero entries in an ndarray

2010-12-22 Thread Alan G Isaac
On 12/22/2010 9:16 AM, Ian Stokes-Rees wrote: > a != 0 > > will be used, but I'm not sure then how to "count" the number of "True" > entries. (a != 0).sum() hth, Alan Isaac ___ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.

[Numpy-discussion] counting non-zero entries in an ndarray

2010-12-22 Thread Ian Stokes-Rees
What is the most efficient way to do the Matlab equivalent of nnz(M) (nnz = number-of-non-zeros function)? I've tried Google, but no luck. My assumption is that something like a != 0 will be used, but I'm not sure then how to "count" the number of "True" entries. TIA. Ian <>__