[Numpy-discussion] numpy-izing a loop
Stéfan van der Walt
stefan@sun.ac...
Tue Feb 10 10:19:54 CST 2009
Hi Paul
2009/2/10 Paul Rudin <paul@rudin.co.uk>:
>
> I've just written this snippet of code:
>
> result = numpy.empty((dim, dim, dim), numpy.bool)
> for x in xrange(dim):
> for y in xrange(dim):
> for z in xrange(dim):
> result[x, y, z] = ((xnear[y, z] < xfar[y, z]) and
> (ynear[x, z] < yfar[x, z]) and
> (znear[x, y] < zfar[x, y]))
>
>
> Is there a way to get numpy to do the looping?
You can try the following:
x = np.arange(dim)
y = np.arange(dim)
z = np.arange(dim)
result = (xnear[y,z] < xfar[y, z]) & (ynear[x, z] < yfar[x, z]) &
(znear[x,y] < zfar[x,y])
It broadcasts correctly, but you'll have to verify the results to be sure.
Cheers
Stéfan
More information about the Numpy-discussion
mailing list