I have a 3D numpy array of shape (3,3,3). I would like to obtain indices of maximum values in a plane,"plane" according to me is as follows:
a = np.random.rand(3,3,3)
>>> a[:,:,0]
array([[0.98423332, 0.44410844, 0.06945133],
[0.69876575, 0.87411547, 0.53595041],
[0.53418486, 0.16186808, 0.60579623]])
>>> a[:,:,1]
array([[0.38969199, 0.80202126, 0.62189662],
[0.66609605, 0.09771614, 0.74061269],
[0.77081531, 0.20068743, 0.72762023]])
>>> a[:,:,2]
array([[0.57110332, 0.29021439, 0.15433043],
[0.21762439, 0.93112448, 0.05763075],
[0.77880124, 0.36637245, 0.29070822]])
I have a solution but I would like to have something shorter and quicker without for loops, my solution is as belows:
for i in range(3):
x=a[:,:,i].argmax()/3
y=a[:,:,i].argmax()%3
z=i
print(x,y,z)
print a[x][y][z]
(0, 0, 0)
0.9842333247061394
(0, 1, 1)
0.8020212566990867
(1, 1, 2)
0.9311244845473187
np.argmax()has anaxiskeyword argument you should look into3x3array of max elements for all theaxiscases i.e 0,1,2.