[Numpy-discussion] Counting array elements

Stephen Walton stephen.walton at csun.edu
Fri Oct 22 22:23:02 CDT 2004

I had no idea my innocent question would generate so much discussion.
Mindful that Perry and Todd are at ADASS in Pasadena next week:

On Fri, 2004-10-22 at 15:18 -0700, Russell E Owen wrote: 
> At 2:35 PM -0700 2004-10-22, Stephen Walton wrote:
> >
> >Why do you say 'numarray is confusing'?  What in the docs would help
> >un-confuse it, in your view?
> - I'll expose my ignorance, but I find the take stuff and fancy 
> indexing nearly incomprehensible.

I agree.  It took me much experimentation to figure out exactly how it
worked.  I'd appreciate it very much if you would look at my suggested
rewrite of this section of the documentation at


and give me any further thoughts for clarification (post them as
comments to the bug report itself).

> - I'd like to write C/C++ code that would work on multiple array 
> types.

I can't help much here, other than to say that C and C++ are pretty low
level languages, not well suited for this level of abstraction.

> - Important functions are sometimes buried in a non-obvious (to me) 
> sub-package.
> For example: try to find that location at which an array has a 
> minimum value

The current index to the documentation seems to include only the
function names but not concepts, which is a problem.  I myself was
trying to remember how to do type conversion;  there is no entry in the
index for 'conversion' or 'coercion' and I finally grepped my local copy
of the HTML files to re-find astype(). 

> - Masked arrays are not integrated.

I haven't tried these yet personally, but I agree that such a feature is
a very important one.  IRAF got partway along on this but didn't finish
it either.

Having said that, my workaround/technique for both MATLAB and numarray
is to simply put NaN's in the places where this not valid data and do
something like


This is MATLAB syntax of course.  Something similar in numarray would go
a long way to helping me.  For example, I have full disk solar images
and I'd like to be able to operate on just the sunspot pixels, or just
the sky pixels, in a straightforward way.

> - For 2-d images x and y are reversed.

Are you referring to the fact that C and numarray are row major and
Fortran is column major?  Or to how images get displayed in the various
plot packages?

> - I keep wanting more support for dealing with arrays of indices, 
> e.g. "give me all the indices for which this is true", then use that 
> to process the data in an array. Numarray seems to do that kind of 
> operation in an entirely different way, suggesting I'm not "with it" 
> on the underlying philosophy.

There are two ways to do this, both of which work.  For example:


will work just as you expect.  A bool array used as an index picks out
those values for which the bool is True.  Essentially identical syntax
now works in MATLAB too.  If you want an index array instead:

>>> index=where(A<7)
>>> A[index]

will do the trick.  For arrays of rank greater than 1:

>>> A=arange(25,shape=(5,5))
>>> where(A<7)
(array([0, 0, 0, 0, 0, 1, 1]), array([0, 1, 2, 3, 4, 0, 1]))

which is a tuple of two arrays that can be used to index A:

>>> ind1,ind2=where(A<7)
>>> A[ind1,ind2]
array([0, 1, 2, 3, 4, 5, 6])
>>> A[ind1,ind2]=[6,5,4,3,2,1,0]	# assignment works too
>>> A
array([[ 6,  5,  4,  3,  2],
       [ 1,  0,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19],
       [20, 21, 22, 23, 24]])

Does this help?

Stephen Walton <stephen.walton at csun.edu>
Physics & Astronomy CSUN

More information about the Numpy-discussion mailing list