Reputation: 43
I have two N dimensional array, the dimension of both arrays are same.
I am trying to filter each of (n,n) array in variable a with (n,n) array in variable b. I am trying this using command
b[a==0 | a>5] = 1
But I am getting following error
IndexError: boolean index did not match indexed array along dimension 2; dimension is 52 but corresponding boolean dimension is 1
I need some help in figuring out as how to filter one N dimensional array using another.
Upvotes: 0
Views: 295
Reputation: 3041
for not experts like me:
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.
[from NumPy Filter Array]
here @AlexanderS.Brunmayr answer for my personal stackoverflow python reference database:
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')
Upvotes: 0