[Numpy-discussion] Geometrically defined masking arrays; how to optimize?
Julian Taylor
jtaylor.debian at googlemail.com
Wed Feb 12 14:23:18 EST 2014
meshgrid also has the sparse keyword argument which archives the same.
On 12.02.2014 20:04, Chris Barker wrote:
> An extra note here:
>
> One of the great things about numpy (as opposed, to say, MATLAB), is
> array broadcasting Thus you generally don't need meshgrid -- why carry
> all that extra repetitive data around. So here's a version that uses
> broadcasting instead
>
>
> radius = 5
>
> # Center at 0, 0
>
> x = np.linspace(-5,5,11)
>
> # y is re-shaped to be a column vector...
> y = np.linspace(-8,8,17).reshape((-1,1))
>
> # when you add x and y you get the 2-d array for the results...
>
> out_circle = (x * x + y * y < radius**2)
>
> print out_circle
>
>
>
>
> On Wed, Feb 12, 2014 at 2:43 AM, Wolfgang Draxinger
> <Wolfgang.Draxinger at physik.uni-muenchen.de
> <mailto:Wolfgang.Draxinger at physik.uni-muenchen.de>> wrote:
>
> On Tue, 11 Feb 2014 22:16:46 +0100
> Daπid <davidmenhur at gmail.com <mailto: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
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org <mailto:NumPy-Discussion at scipy.org>
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>
>
>
> --
>
> Christopher Barker, Ph.D.
> Oceanographer
>
> Emergency Response Division
> NOAA/NOS/OR&R (206) 526-6959 voice
> 7600 Sand Point Way NE (206) 526-6329 fax
> Seattle, WA 98115 (206) 526-6317 main reception
>
> Chris.Barker at noaa.gov <mailto:Chris.Barker at noaa.gov>
>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
More information about the NumPy-Discussion
mailing list