[SciPy-user] howto extract positions and values of non-zeros from array (flat)?

dmitrey openopt@ukr....
Thu Aug 30 07:58:22 CDT 2007

I have one-dimensional array with zeros and non-zeros, like
a = array((1,0,2,3,4,0,5,0))

I need to obtain positions of non-zeros and corresponding values
ind = array((0, 2, 3,4, 6))
val = array((1,2,3,4,5))

what's the simplest way, that uses only numpy?

in MATLAB it would look like [ind, val] = find(a)

Regards, D.

More information about the SciPy-user mailing list