【问题标题】:Selecting a part of a 2d array of data depending on two 2d arrays根据两个二维数组选择二维数据数组的一部分
【发布时间】:2022-10-05 00:01:59
【问题描述】:

我有三个数组x, y, data,

print(x.shape, y.shape, data.shape)

(565, 1215) (565, 1215) (565, 1215)

借此:

print(x.min(), y.min(), data.min(), x.max(), y.max(), data.max())

-55.530094 33.582264 0.0 55.530094 66.823235 275.67851091467816

如何从二维数组data 中选择值((x>=-20) & (x<=20) & (y>=35) & (y<=60))

我尝试了以下方法:

indices = np.where((x>=-20) &  (x<=20) & (y>=35) &  (y<=60))

print(indices)

(array([ 28,  28,  28, ..., 540, 540, 540], dtype=int64), array([ 35,  36,  37, ..., 671, 672, 673], dtype=int64))

如何将此索引应用于data

【问题讨论】:

    标签: python numpy


    【解决方案1】:

    只需data[indices]

    例子:

    import numpy as np
    
    x = np.random.uniform(0, 100, size=(100, 100))
    y = np.random.uniform(0, 100, size=(100, 100))
    data = np.random.randint(0, 1000, size=(100, 100))
    
    indices = np.where((x >= -20) & (x <= 20) & (y >= 35) & (y <= 60))
    print(data[indices])
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-03-09
      • 1970-01-01
      • 2021-10-29
      • 2011-07-24
      • 2014-11-17
      • 2014-04-03
      • 1970-01-01
      相关资源
      最近更新 更多