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

Thomas K Gamble tkg@lanl....
Wed Dec 22 11:43:55 CST 2010


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 sure then how to "count" the number of "True"
> entries.
> 
> TIA.
> 
> Ian

one possibility:

len(where(a != 0)[0])

-- 
Thomas K. Gamble
Research Technologist, System/Network Administrator
Chemical Diagnostics and Engineering (C-CDE)
Los Alamos National Laboratory
MS-E543,p:505-665-4323 f:505-665-4267

There cannot be a crisis next week. My schedule is already full.
    Henry Kissinger


More information about the NumPy-Discussion mailing list