[Numpy-discussion] Geometrically defined masking arrays; how to optimize?
Wolfgang Draxinger
Wolfgang.Draxinger at physik.uni-muenchen.de
Wed Feb 12 05:43:37 EST 2014
On Tue, 11 Feb 2014 22:16:46 +0100
Daπid <davidmenhur at gmail.com> wrote:
> Here it is an example:
>
> import numpy as np
> import pylab as plt
>
> N = 100
> M = 200
> x, y = np.meshgrid(np.arange(N), np.arange(M))
>
> # Center at 40, 70, radius 20
> x -= 40
> y -= 70
> out_circle = (x * x + y * y < 20**2)
Neat.
> Note that I have avoided taking the costly square root of each
> element by just taking the square of the radius. It can also be
> generalised to ellipses or rectangles, if you need it.
Oops, why did I take the "long" tour anyway?
> Also, don't use 0 as a False value, and don't force it to be a 0.
> Instead, use "if not ring:"
'ring' is not a boolean, but a numeric index and the test is if the
index is nonzero. I could have also written 'if ring > 0:' and swapped
the clauses. Yes I know that for a value 0 zero it will test as not,
but in this case I wanted to have it written down, that this tests for
a certain numerical value.
Thanks,
Wolfgang
More information about the NumPy-Discussion
mailing list