[SciPy-User] Creating meshgrid from meshgrid

Florian Lindner mailinglists at xgm.de
Wed Sep 6 00:31:29 EDT 2017


Hey,

I'm sorry, I realized my posting was confusing, tried to rewrite it:


In 1-d I can easily and elegantly achieve what I want, but I'm unable to transfer it to 2-d or even higher dimensional

**1-d**

I have two 1-d meshes of size N = 3.

    a = [1, 2, 3]
    b = [10, 20, 30]

now I want to evaluate a function fun on on the difference/norm of all pairs of these meshes:

    def fun(x):
        return x

    mgrid = np.meshgrid( a,b )
    A = fun( np.abs(mgrid[0] - mgrid[1] ) )

Result A is of size N x N:

    array([[ 9,  8,  7],
           [19, 18, 17],
           [29, 28, 27]])

which is

    |b[0]-a[0]| |b[0]-a[1]| |b[0]-a[2]|
    |b[1]-a[0]| |b[1]-a[1]| |b[1]-a[2]|
    |b[2]-a[0]| |b[2]-a[1]| |b[2]-a[2]|

(only arguments of fun, for sake of brevity)

**2-d**

Now in 2-d function fun stays the same.

I have again two meshes a and b

    ax = [1, 2, 3]
    ay = [4, 5, 6]
    bx = [10, 20, 30]
    by = [40, 50, 60]

    a = np.meshgrid(ax, ay)
    b = np.meshgrid(bx, by)

Now, how can I do the same I did with the 1-d meshes above for 2-d and possibly also for higher dimensions?

The first row of A:

    || (10 40) - (1 4) || || (10 40) - (1 5) || || (10 40) - (1 6) ||
    || (10 40) - (2 4) || || (10 40) - (2 5) || || (10 40) - (2 6) ||
    || (10 40) - (3 4) || || (10 40) - (3 5) || || (10 40) - (3 6) ||

(everything is jus the first row of A, again only arguments of fun)

The result mesh should have the size N * N x N * N.

I tried to create the coordinates of a and b

    ca = np.array(list(zip(a[0].flatten(), a[1].flatten())))
    cb = np.array(list(zip(b[0].flatten(), b[1].flatten())))

And create a meshgrid from that:

    mgrid = np.meshgrid([ca], [cb])

but alone the dimensionality does not fit (18 instead of 9).

I hope I was now able to better get across what I want.

Thanks!
Florian


Am 06.09.2017 um 07:54 schrieb Chris Barker:
> I'm confused as to what you are trying to do.
> 
> Meshgrid is used to create a regular grid when you have a vectors of the axes.
> 
> It support 2 or more dimensions.
> 
>     I have two arrays of size N (let's say 2 arrays of length 4) that I combine using np.meshgrid
> 
>     xxA, yyA = np.meshgrid(xA, yA)
>     xxB, yyB = np.meshgrid(xB, yB)
> 
>     which gives me two meshes
> 
>     xx.shape = yy.shape = (4,4)
>     which represent a N-dimensional mesh with 16 elements.
> 
> 
> no -- it represents a 2-dimensional mesh with four nodes in each direction.
>  
> 
>     Now I want to evaluate a function f on every possible pair of N-dimensional points in the grid, resulting in a 16 x 16
>     matrix:
> 
> 
> I think you are looking for a different function than meshgrid.
> 
> But if you want to evaluate a function on a four dimensional space, you can use meshgrid with four dimensions:
> 
> xx, yy, zz, tt = meshgrid(x, y, z, t)
> 
> results = func(xx,yy,zz,tt)
> 
> note that with numpy's broadcasting, you may not need to use meshgrid at all.
> 
> Is that what you are looking for?
> 
> -CHB
> 
> 
> -- 
> 
> 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>
> 
> 
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at python.org
> https://mail.python.org/mailman/listinfo/scipy-user
> 


More information about the SciPy-User mailing list