0

我有两个 N 维数组,两个数组的维数相同。

  1. a = (n,n,52)
  2. b = (n,n,52)

我正在尝试使用变量 b 中的 (n,n) 数组过滤变量a 中的 (n,n) 数组每一个。我正在尝试使用命令

b[a==0 | a>5] = 1

但我收到以下错误

IndexError: boolean index did not match indexed array along dimension 2; dimension is 52 but corresponding boolean dimension is 1

我需要一些帮助来弄清楚如何使用另一个过滤一个 N 维数组。

4

1 回答 1

0

对于像我这样的非专家:

Filtering Arrays: Getting some elements out of an existing array and creating a new array out of them is called filtering. In NumPy, you filter an array using a boolean index list. If the value at an index is True that element is contained in the filtered array, if the value at that index is False that element is excluded from the filtered array. [来自NumPy 过滤器数组]

这里@AlexanderS.Brunmayr 回答我的个人stackoverflow python 参考数据库:


import numpy as np

n= 2

a = np.zeros((n,n,2))

b = np.zeros((n,n,2))

a[1,1,0] = 3
a[1,1,1]= 7


print('a : \n',a,'\n b : \n',b,'\n')



print('\n filter : \n',[(a==0) | (a>5)])  ## array filter

b[(a==0) | (a>5)] = 1  ### ------> change b[a==0 | a>5]  to b[(a==0) | (a>5)]


print('a : \n',a,'\n b : \n',b,'\n')
于 2021-08-01T07:59:01.430 回答