[Numpy-discussion] Understanding mgrid
Brad Malone
brad.malone@gmail....
Fri Sep 19 14:13:25 CDT 2008
Thanks for the response Robert.
So, at least in this case, the results of mgrid (or indices) only provides
information about the spacing of the grid and not on the absolute value of
the point coordinates?
In your example, is there a way to see within your x[ix], y[iy], and z[iz]
matrices the same collection of points that you would see if you did
something like the following?
points=[]
x=linspace(0,1,3)
y=linspace(1,2.5,4)
z=linspace(3,5,5)
for k in z.tolist():
for j in y.tolist():
for i in x.tolist():
point=array([i,j,k])
points.append(point)
Thanks,
Brad
On Fri, Sep 19, 2008 at 11:22 AM, Robert Kern <robert.kern@gmail.com> wrote:
> On Fri, Sep 19, 2008 at 12:59, Brad Malone <brad.malone@gmail.com> wrote:
> > Hi, I was wondering if someone could englighten me on what the
> geometrical
> > significance of numpy.mgrid is. I can play around with it and see trends
> in
> > the sizes and number of arrays, but why does it give the output that it
> > does? Looking at the example shown below, why does it return a matrix and
> > its transpose?
>
> Well, it returns one array. In your example, there is a (2,5,5) array,
> which is basically the concatenation of two arrays which *happen* to
> be transposes of each other. If you had chosen differently sized axes,
> they wouldn't be transposes.
>
> In [14]: mgrid[0:2,0:3]
> Out[14]:
> array([[[0, 0, 0],
> [1, 1, 1]],
>
> [[0, 1, 2],
> [0, 1, 2]]])
>
> > Is this a representation of some geometrical grid?
>
> It can be. There are other uses for it.
>
> > Does the
> > output imply some sort of connectivity?
>
> It describes an orthogonal grid.
>
> > If so, how do you see it?
> >
> > >>> mgrid[0:5,0:5]
> > array([[[0, 0, 0, 0, 0],
> > [1, 1, 1, 1, 1],
> > [2, 2, 2, 2, 2],
> >
> > [3, 3, 3, 3, 3],
> >
> > [4, 4, 4, 4, 4]],
> > <BLANKLINE>
> > [[0, 1, 2, 3, 4],
> >
> > [0, 1, 2, 3, 4],
> > [0, 1, 2, 3, 4],
> > [0, 1, 2, 3, 4],
> >
> >
> > [0, 1, 2, 3, 4]]])
> >
> >
> >
> > I have a cubic grid in 3D space that is spanned by 3 orthogonal vectors.
> Am
> > I able to generate this equivalent grid with mgrid somehow? If so, how is
> it
> > done? I am using mayavi and I need to be able to construct some arrays in
> > the same way that mgrid would have constructed them, so this is why I
> ask.
>
> I would probably use indices() instead of mgrid if you are just given
> the x, y, and z vectors. indices([n,m,k]) is equivalent to
> mgrid[0:n,0:m,0:k]:
>
>
> In [19]: x = linspace(0, 1, 3)
>
> In [20]: x
> Out[20]: array([ 0. , 0.5, 1. ])
>
> In [21]: y = linspace(1, 2.5, 4)
>
> In [22]: y
> Out[22]: array([ 1. , 1.5, 2. , 2.5])
>
> In [23]: z = linspace(3, 5, 5)
>
> In [24]: z
> Out[24]: array([ 3. , 3.5, 4. , 4.5, 5. ])
>
> In [25]: ix, iy, iz = indices([len(x), len(y), len(z)])
>
> In [26]: x[ix]
> Out[26]:
> array([[[ 0. , 0. , 0. , 0. , 0. ],
> [ 0. , 0. , 0. , 0. , 0. ],
> [ 0. , 0. , 0. , 0. , 0. ],
> [ 0. , 0. , 0. , 0. , 0. ]],
>
> [[ 0.5, 0.5, 0.5, 0.5, 0.5],
> [ 0.5, 0.5, 0.5, 0.5, 0.5],
> [ 0.5, 0.5, 0.5, 0.5, 0.5],
> [ 0.5, 0.5, 0.5, 0.5, 0.5]],
>
> [[ 1. , 1. , 1. , 1. , 1. ],
> [ 1. , 1. , 1. , 1. , 1. ],
> [ 1. , 1. , 1. , 1. , 1. ],
> [ 1. , 1. , 1. , 1. , 1. ]]])
>
> In [27]: y[iy]
> Out[27]:
> array([[[ 1. , 1. , 1. , 1. , 1. ],
> [ 1.5, 1.5, 1.5, 1.5, 1.5],
> [ 2. , 2. , 2. , 2. , 2. ],
> [ 2.5, 2.5, 2.5, 2.5, 2.5]],
>
> [[ 1. , 1. , 1. , 1. , 1. ],
> [ 1.5, 1.5, 1.5, 1.5, 1.5],
> [ 2. , 2. , 2. , 2. , 2. ],
> [ 2.5, 2.5, 2.5, 2.5, 2.5]],
>
> [[ 1. , 1. , 1. , 1. , 1. ],
> [ 1.5, 1.5, 1.5, 1.5, 1.5],
> [ 2. , 2. , 2. , 2. , 2. ],
> [ 2.5, 2.5, 2.5, 2.5, 2.5]]])
>
> In [28]: z[iz]
> Out[28]:
> array([[[ 3. , 3.5, 4. , 4.5, 5. ],
> [ 3. , 3.5, 4. , 4.5, 5. ],
> [ 3. , 3.5, 4. , 4.5, 5. ],
> [ 3. , 3.5, 4. , 4.5, 5. ]],
>
> [[ 3. , 3.5, 4. , 4.5, 5. ],
> [ 3. , 3.5, 4. , 4.5, 5. ],
> [ 3. , 3.5, 4. , 4.5, 5. ],
> [ 3. , 3.5, 4. , 4.5, 5. ]],
>
> [[ 3. , 3.5, 4. , 4.5, 5. ],
> [ 3. , 3.5, 4. , 4.5, 5. ],
> [ 3. , 3.5, 4. , 4.5, 5. ],
> [ 3. , 3.5, 4. , 4.5, 5. ]]])
>
> --
> Robert Kern
>
> "I have come to believe that the whole world is an enigma, a harmless
> enigma that is made terrible by our own mad attempt to interpret it as
> though it had an underlying truth."
> -- Umberto Eco
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion@scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20080919/13799993/attachment.html
More information about the Numpy-discussion
mailing list