# [SciPy-User] Creating meshgrid from meshgrid Classic List Threaded 5 messages Open this post in threaded view
|

## [SciPy-User] Creating meshgrid from meshgrid

 Hello, yes, the subject sound a bit weird... 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. 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: in a flattened notation, pA = (xxA, yyA) f(pA-pB) f(pA-pB) f(pA-pB) ... f(pA-pB) f(pA-pB) f(pA-pB) ... f(pA-pB) f(pA-pB) f(pA-pB) ... . . . Let's say xA = yA = [1,2,3] and xB = yB = [10,20,30] that gives me a mesh A: (1,3) (2,3) (3,3) (1,2) (2,2) (3,2) (1,1) (2,1) (3,1) and a mesh B alike. My result matrix now should be of size 9 x 9: f( (1,3), (10,30) ) f( (2,3), (20,30) ) f( (3,3), (30, 30) ) f( (1,2), (10,20) ) f( (2,2), (20,20) ) f( (3,2), (30, 20) ) ... f always takes two N-dimensional vectors and returns a scalar. I hope I was able to explain what I want to achieve. What is the best way to do that in numpy/scipy? As long as the meshes itself are 1-d I did it like that: mgrid = np.meshgrid([1,2,3], [10,20,30]) A = f( np.abs(mgrid - mgrid) ) Thanks, Florian _______________________________________________ SciPy-User mailing list [hidden email] https://mail.python.org/mailman/listinfo/scipy-user
Open this post in threaded view
|

## Re: Creating meshgrid from meshgrid

 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.OceanographerEmergency Response DivisionNOAA/NOS/OR&R            (206) 526-6959   voice7600 Sand Point Way NE   (206) 526-6329   faxSeattle, WA  98115       (206) 526-6317   main reception[hidden email] _______________________________________________ SciPy-User mailing list [hidden email] https://mail.python.org/mailman/listinfo/scipy-user
Open this post in threaded view
|

## Re: Creating meshgrid from meshgrid

 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 - mgrid ) ) Result A is of size N x N:     array([[ 9,  8,  7],            [19, 18, 17],            [29, 28, 27]]) which is     |b-a| |b-a| |b-a|     |b-a| |b-a| |b-a|     |b-a| |b-a| |b-a| (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.flatten(), a.flatten())))     cb = np.array(list(zip(b.flatten(), b.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 > > [hidden email] > > > _______________________________________________ > SciPy-User mailing list > [hidden email] > https://mail.python.org/mailman/listinfo/scipy-user> _______________________________________________ SciPy-User mailing list [hidden email] https://mail.python.org/mailman/listinfo/scipy-user