i want to make a function that will return an array of booleans
. Basically the code will check the equality of elements inside a 4-D array. It will through 2nd and 3rd dimension and compare every array whether they all are equal or not. Here's my current code :
x = np.arange(0,72).reshape(4,2,3,3)
x[:,1,2,:] = 0
def equality(arr):
return np.array([np.allclose(arr[:,i,j,:],arr[:,i,j,:+1]) for i in range(arr.shape[1]) for j in range(arr.shape[2])])
print(x)
print(equality(x))
Output :
[[[[ 0 1 2] [ 3 4 5] [ 6 7 8]]
[[ 9 10 11] [12 13 14] [ 0 0 0]]]
[[[18 19 20] [21 22 23] [24 25 26]]
[[27 28 29] [30 31 32] [ 0 0 0]]]
[[[36 37 38] [39 40 41] [42 43 44]]
[[45 46 47] [48 49 50] [ 0 0 0]]]
[[[54 55 56] [57 58 59] [60 61 62]]
[[63 64 65] [66 67 68] [ 0 0 0]]]] [False False False False
False True]
That code works, but is there a way to get rid of that for loop using numpy stuff instead?