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

dmitrey openopt at ukr.net
Thu Aug 30 08:58:22 EDT 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