[Numpy-discussion] Counting array elements

Stephen Walton stephen.walton at csun.edu
Thu Oct 21 10:59:05 CDT 2004

Is there some simple way of counting the number of array elements which
satisfy a certain condition?  It is easy to do


to sum all the values of A which are less than 1, but there doesn't seem
to be a count() method.  I tried


but this throws an exception at numarray 1.1.  If I try


I have to nest multiple sums if A has rank greater than 1, plus the sum
overflows if A is large, apparently because boolean gets treated as
Int8.  (Try A=arange(1024,shape=(32,32));sum(sum(A<=1024)).  You get
zero.)  The following works:


but is awkward.  Am I missing an obvious better alternative?  If not,
I'm going to file an RFE :-) .

Stephen Walton <stephen.walton at csun.edu>
Dept. of Physics & Astronomy, Cal State Northridge
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
Url : http://projects.scipy.org/pipermail/numpy-discussion/attachments/20041021/85482632/attachment.bin 

More information about the Numpy-discussion mailing list