Numpy meshgrid in 3D

前端 未结 7 1210
暖寄归人
暖寄归人 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:27

    Numpy (as of 1.8 I think) now supports higher that 2D generation of position grids with meshgrid. One important addition which really helped me is the ability to chose the indexing order (either xy or ij for Cartesian or matrix indexing respectively), which I verified with the following example:

    import numpy as np
    
    x_ = np.linspace(0., 1., 10)
    y_ = np.linspace(1., 2., 20)
    z_ = np.linspace(3., 4., 30)
    
    x, y, z = np.meshgrid(x_, y_, z_, indexing='ij')
    
    assert np.all(x[:,0,0] == x_)
    assert np.all(y[0,:,0] == y_)
    assert np.all(z[0,0,:] == z_)
    

提交回复
热议问题