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

Alexander Michael lxander.m@gmail....
Thu Aug 30 09:00:54 CDT 2007


On 8/30/07, dmitrey <openopt@ukr.net> wrote:
> 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)

There's also nonzero:
<http://www.scipy.org/Numpy_Example_List_With_Doc#head-54557a8d2e7bd247c840de248fe532997635d180>


More information about the SciPy-user mailing list