How to convert the output of meshgrid to the corresponding array of points?

前端 未结 3 2026
自闭症患者
自闭症患者 2021-01-30 18:19

I want to create a list of points that would correspond to a grid. So if I want to create a grid of the region from (0, 0) to (1, 1), it would contain

3条回答
  •  再見小時候
    2021-01-30 18:55

    Are your gridpoints always integral? If so, you could use numpy.ndindex

    print list(np.ndindex(2,2))
    

    Higher dimensions:

    print list(np.ndindex(2,2,2))
    

    Unfortunately, this does not meet the requirements of the OP since the integral assumption (starting with 0) is not met. I'll leave this answer only in case someone else is looking for the same thing where those assumptions are true.


    Another way to do this relies on zip:

    g = np.meshgrid([0,1],[0,1])
    zip(*(x.flat for x in g))
    

    This portion scales nicely to arbitrary dimensions. Unfortunately, np.meshgrid doesn't scale well to multiple dimensions, so that part will need to be worked out, or (assuming it works), you could use this SO answer to create your own ndmeshgrid function.

提交回复
热议问题