[英]Find each row's maximum and index in 3-dimentional matrix in python
import numpy as np
a=np.zeros(shape=(3,4,5))
print(type(a))
a[0][1]=1,2,3,4,5
a[1][3]=6,7,8,9,12
a[2][1]=12,34,54,21,89
a
is the following matrix: a
是以下矩阵:
[[[ 0. 0. 0. 0. 0.]
[ 1. 2. 3. 4. 5.]
[ 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0.]]
[[ 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0.]
[ 6. 7. 8. 9. 12.]]
[[ 0. 0. 0. 0. 0.]
[12. 34. 54. 21. 89.]
[ 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0.]]]
. .
print(a.max(axis=0))
print(a.argmax(axis=0))
I can get the matrix results:我可以得到矩阵结果:
[[ 0. 0. 0. 0. 0.]
[12. 34. 54. 21. 89.]
[ 0. 0. 0. 0. 0.]
[ 6. 7. 8. 9. 12.]]
[[0 0 0 0 0]
[2 2 2 2 2]
[0 0 0 0 0]
[1 1 1 1 1]]
, but how can I get the whole index of each maximum like [0,0,1] or something, as I need the index to draw a 3D figure. ,但是如何获得每个最大值的整个索引,例如 [0,0,1] 或其他东西,因为我需要索引来绘制 3D 图形。
If I understood you correctly, you'd like to get the index of the maximum along the last two axes.如果我理解正确的话,您会希望获得沿最后两个轴的最大值的索引。 This can be done like this
这可以像这样完成
np.array([[i, j, np.argmax(a[i, j])] for i in range(a.shape[0]) for j in range(a.shape[1])])
yielding屈服
array([[0, 0, 0],
[0, 1, 4],
[0, 2, 0],
[0, 3, 0],
[1, 0, 0],
[1, 1, 0],
[1, 2, 0],
[1, 3, 4],
[2, 0, 0],
[2, 1, 4],
[2, 2, 0],
[2, 3, 0]])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.