[Numpy-discussion] Apply a function to all indices

Ernest Adrogué eadrogue@gmx....
Fri Feb 26 07:02:41 CST 2010


26/02/10 @ 13:31 (+0100), thus spake Ole Streicher:
> Hello Ernest,
> 
> Ernest Adrogué <eadrogue@gmx.net> writes:
> > It depends on what exactly you want to do. If you just want
> > to iterate over the array, try something liks this
> > for element in a[a > 0.8]:
> > 	myfunc(element)
> 
> No; I need to iterate over the *indices*, not over the elements.
> 
> a = numpy.random.random((2,3,4))
> 
> for index in ???(a > 0.5):
>     print index[0], index[1], index[2]

Ah, I think np.argwhere does that:

for index in numpy.argwhere(a > 0.5):
	print index

index is an array, so it can be indexed, for example:

for index in numpy.argwhere(a > 0.5):
	print a[index[0], index[1], index[2]]

prints all elements in a that are greater than 0.5.

Bye.

> Best regards
> 
> Ole
> 
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion


More information about the NumPy-Discussion mailing list