I have a first 3D numpy array and a second numpy array containing the locations (coordinates) of elements of the first I am interested in.
first = np.array([[[100, 101, 102],
[103, 104, 105],
[106, 107, 108]],
[[109, 110, 111],
[112, 113, 114],
[115, 116, 117]],
[[118, 119, 120],
[121, 122, 123],
[124, 125, 126]]])
second = np.array([[0, 1, 0],
[1, 1, 0],
[1, 0, 0],
[0, 0, 0],
[0, 1, 1],
[1, 1, 1],
[1, 0, 1],
[0, 0, 1]])
result = np.array([103, 112, 109, 100, 104, 113, 110, 101])
The result I would like: all values located at the positions contained in the second array.
Using first[second] does not do the trick. I would like to avoid looping.
np.where(second)to then perform an access tofirstfrom the result?first[np.where(second)]does not work either.firstand what the expected output should be. "It does not work" is unacceptable.secondsupposed to index? The relation of your 2dsecondto a unknown 3d is not clear. You may have to show "looping" code first to show us clearly what you are trying to do.