import numpy as np
# The 3D arrays have the axis: Z, X, Y
arr_keys = np.random.rand(20, 5, 5)
arr_vals = np.random.rand(20, 5, 5)
arr_idx = np.random.rand(5, 5)
For each grid cell in arr_idx
, I want to look up the Z-position of the value closest to it in arr_keys
(but with the same X, Y location) and return the value at the corresponding position in arr_vals
array. Is there a way to do this without using nested for loops?
So, if the value at X=0, Y=0 for arr_idx
is 0.5, I want to find the number closest to it at X=0, Y=0, Z ranges from 0 to 10
in arr_keys
, and then I want to use the Z position of that number (lets call it Z_prime) to find the value in arr_vals
(Z_prime, X=0, Y=0)