Thursday 15 July 2010

python - Numpy meshgrid in 3D -


Namsexual meshgrid is very useful for converting two vectors to a synced grid. It is the easiest to extend it to three dimensions. What's the way So given the three vectors X, Y, and Z, the creation of 3x3D arrays (instead of 2x2D arrays) which can be used as coordinates.

postprop = "itemprop =" Meshgrid's source code:

  def meshgrid (x, y): "" "two Return coordinates matrix from coordinating vectors "Parameters ---------- x, y: Two 1-D arrays representing the x and y coordinate of the grid. Returns ------- X, Y For `ndarray vectors` x``,` y 'with `y` length,` `xx`` = lane (x)` `and` `ne = lane (y)` `, return` x', `y ', where` x' And`Y '`` (NE, NX) `` `` `` `` `` ` The element of y, the second for `y`, besides-------- index_tricks.mgrid: using a multi-dimensional" mashgrid "indexing notation index.tricks.ogrid: the creation of an open multidimensional" mashgrid " Examples of using indexing notation -------->>>> X, Y = N. P. Mesegrid ([1,2,3], [4,5,6,7])> Gt; & gt; & gt; X Array ([[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]) Yesh array ([4, 4, 4], [5, 5, 5], [6, 6, 6], [7, 7, 7]]) to evaluate the function on the grid; `meshgrid `Very useful & Gt; & Gt; & Gt; X = np.arange (-5, 5, 0.1)> gt; & Gt; & Gt; Y = NP Color (-5, 5, 0.1)> gt; & Gt; & Gt; Xx, yy = np.meshgrid (x, y)> gt; & Gt; & Gt; Z = np.sin (xx ** 2 + yy ** 2) / (xx ** 2 + yy ** 2) "" "x = asarray (x) y = asarray (y) numrows, numCols = lane ), Lan (x) # yeah, back x = x.reshape (1, numcols) x = x.repeat (numres, axis = 0) y = y.reshape (numres, 1) y = y.repeat (numcols, Axis = 1) Returns X, Y  

This is quite easy to understand. I have extended the pattern to an arbitrary number of dimensions, but this code is not optimized in any way ( And not completely error, hope it helps you:

  def meshgrid2 (* AR): arrs = tuple (reverse (aria) )) #edit lens = map (len, ARM) dim = LAN (ARI) sz = 1 in lens s: sz * = s ans = [] i, Earn an ARM: ARA = SLC = [1] * Slow SLC [i] = lens [i] Arr2 = asarray (arr). Receiver for JS (SLC), Siege in Enzરેટ (lens): If J! = I: arr2 = arr2.repeat (sz, axis = j) Ans.append (arr2) returns to (ans)  

No comments:

Post a Comment