【问题标题】:Find matching rows in a numpy matrix of 3在 3 的 numpy 矩阵中查找匹配行
【发布时间】:2020-04-24 03:54:20
【问题描述】:

给定一个 mxmxm 的立方体,我需要知道 6 个面中的行,它们的行中的最小值大于给定的 n。

【问题讨论】:

  • np.min>[] 的某种组合,针对不同的轴重复此操作 6 次。
  • Err... 你的“行”在什么方向?这里没有指明方向的规范依据。

标签: python-3.x numpy cube


【解决方案1】:

获取各种面孔:

faces = np.array([
    x[ 0,  :,  :],
    x[-1,  :,  :],
    x[ :,  0,  :],
    x[ :, -1,  :],
    x[ :,  :,  0],
    x[ :,  :, -1],
])

现在折叠最后一个维度轴:

# No information on orientation provided by OP so always pick axis=-1
row_mins = np.min(faces, axis=-1)

然后只保留满足条件的行:

valid_rows = faces[row_mins > n]
print(valid_rows)

【讨论】:

    【解决方案2】:

    您可以在迭代的帮助下过滤值。对于numpy iteration

    代码

    import numpy as np
    data=np.arange(54).reshape(6,3,3)
    print(data,data.ndim)
    
    #n : given value to filter
    n=10
    
    #to get all the elements that are greater than n
    print(data[data>n])
    
    for i in data:
      for row in i:
        if  row[row>n].size :
            print(row)
    

    如果您有任何疑问,请告诉我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-05
      • 1970-01-01
      • 2021-12-12
      • 1970-01-01
      • 1970-01-01
      • 2018-01-31
      • 2019-08-31
      相关资源
      最近更新 更多