将numpy开放网格转换为坐标 [英] convert numpy open mesh to coordinates

查看:125
本文介绍了将numpy开放网格转换为坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将numpy ix_例程返回的开放网格转换为坐标列表

I'd like to turn an open mesh returned by the numpy ix_ routine to a list of coordinates

例如,用于:

In[1]: m = np.ix_([0, 2, 4], [1, 3])
In[2]: m
Out[2]: 
(array([[0],
        [2],
        [4]]), array([[1, 3]]))

我想要的是:

([0, 1], [0, 3], [2, 1], [2, 3], [4, 1], [4, 3])

我敢肯定,我可以将其与一些迭代,解压缩和压缩一起进行破解,但是我敢肯定,必须有一种聪明的,麻木的方式来实现这一目标...

I'm pretty sure I could hack it together with some iterating, unpacking and zipping, but I'm sure there must be a smart numpy way of achieving this...

推荐答案

方法1 使用

Approach #1 Use np.meshgrid and then stack -

r,c = np.meshgrid(*m)
out = np.column_stack((r.ravel('F'), c.ravel('F') ))


方法2 或者,依次使用np.array()transposingreshaping-


Approach #2 Alternatively, with np.array() and then transposing, reshaping -

np.array(np.meshgrid(*m)).T.reshape(-1,len(m))

对于具有np.ix_中使用的通用数组数的通用情况,这是需要的修改-

For a generic case with for generic number of arrays used within np.ix_, here are the modifications needed -

p = np.r_[2:0:-1,3:len(m)+1,0]
out = np.array(np.meshgrid(*m)).transpose(p).reshape(-1,len(m))

样品运行-

两个数组的情况:

In [376]: m = np.ix_([0, 2, 4], [1, 3])

In [377]: p = np.r_[2:0:-1,3:len(m)+1,0]

In [378]: np.array(np.meshgrid(*m)).transpose(p).reshape(-1,len(m))
Out[378]: 
array([[0, 1],
       [0, 3],
       [2, 1],
       [2, 3],
       [4, 1],
       [4, 3]])

三个数组的情况:

In [379]: m = np.ix_([0, 2, 4], [1, 3],[6,5,9])

In [380]: p = np.r_[2:0:-1,3:len(m)+1,0]

In [381]: np.array(np.meshgrid(*m)).transpose(p).reshape(-1,len(m))
Out[381]: 
array([[0, 1, 6],
       [0, 1, 5],
       [0, 1, 9],
       [0, 3, 6],
       [0, 3, 5],
       [0, 3, 9],
       [2, 1, 6],
       [2, 1, 5],
       [2, 1, 9],
       [2, 3, 6],
       [2, 3, 5],
       [2, 3, 9],
       [4, 1, 6],
       [4, 1, 5],
       [4, 1, 9],
       [4, 3, 6],
       [4, 3, 5],
       [4, 3, 9]])

这篇关于将numpy开放网格转换为坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆