Numpy meshgrid in 3D

前端 未结 7 1191
暖寄归人
暖寄归人 2020-11-27 15:45

Numpy\'s meshgrid is very useful for converting two vectors to a coordinate grid. What is the easiest way to extend this to three dimensions? So given three vectors x, y, an

7条回答
  •  余生分开走
    2020-11-27 16:05

    Here is a multidimensional version of meshgrid that I wrote:

    def ndmesh(*args):
       args = map(np.asarray,args)
       return np.broadcast_arrays(*[x[(slice(None),)+(None,)*i] for i, x in enumerate(args)])
    

    Note that the returned arrays are views of the original array data, so changing the original arrays will affect the coordinate arrays.

提交回复
热议问题