0

I want to find the position in a 3D numpy array where a 2D array first exceeds the value in the 3D array. E.g.

import numpy as np
array_3D = np.random.rand(10, 3, 3)
array_2D = np.random.rand(3, 3)

np.argmax(array_2D > array_3D)

I get the result 0, but I want a 2D array i.e. essentially argmax results for each grid cell.

2 Answers 2

2

If I understand you correctly, that is what the axis parameter is for:

>>>np.argmax(array_2D > array_3D,axis=0)
array([[1, 0, 0],
       [0, 2, 1],
       [0, 2, 0]])

otherwise it runs across the flat array.

Sign up to request clarification or add additional context in comments.

Comments

0

np.where(array_2D > array_3D) will give you 3 arrays with the coordinates of the points you are looking for. If you are looking for the first point:

x =  np.where(array_2D > array_3D)[0][0]
y =  np.where(array_2D > array_3D)[1][0]
z =  np.where(array_2D > array_3D)[2][0]

Certainly not the most elegant, thought, as you won't have directly the first point.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.