I have an array A. I have an another array which I call switch. Array switch, is an array of boolean values (and np.nan which correspond to location of np.nan 3 x 3 arrays in A) that determines which 3 x 3 array in A gets selected for an operation.
Example:
switch = np.array([ True, False, False, False, np.nan])
a = np.array([[[ 0., 0., 0.],
[ 9., 0., 0.],
[ 3., 15., 0.]],
[[ 0., 0., 0.],
[ 9., 0., 0.],
[ 3., 15., 0.]],
[[ 0., 27., 0.],
[ 0., 0., 0.],
[12., 18., 0.]],
[[ 0., 0., 6.],
[12., 0., 27.],
[ 0., 0., 0.]],
[[ np.nan, np.nan, np.nan],
[np.nan, np.nan, np.nan],
[ np.nan, np.nan, np.nan]]])
The final selected array based on the example above should be:
np.array([[[ 0., 0., 0.],
[ 9., 0., 0.],
[ 3., 15., 0.]],
[[ np.nan, np.nan, np.nan],
[np.nan, np.nan, np.nan],
[ np.nan, np.nan, np.nan]],
[[ np.nan, np.nan, np.nan],
[np.nan, np.nan, np.nan],
[ np.nan, np.nan, np.nan]],
[[ np.nan, np.nan, np.nan],
[np.nan, np.nan, np.nan],
[ np.nan, np.nan, np.nan]],
[[ np.nan, np.nan, np.nan],
[np.nan, np.nan, np.nan],
[ np.nan, np.nan, np.nan]]])
I can do this by using a for loop and iterating through each 3 x 3 array in A with the use of if/else control statements but I am wondering if there is a way to do this using the built in functionality within numpy. For loops and if/else statements is likely much slower than array manipulation. I've been tinkering but so far have not been able to find an optimal numpy solution.
I can restructure array switch but array A I cannot change the structure/shape.