简体   繁体   English

在python中的3维矩阵中查找每一行的最大值和索引

[英]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.

相关问题 给定一个类型为`scipy.sparse.coo_matrix`的矩阵如何确定每行的最大值的索引和值? - Given a matrix of type `scipy.sparse.coo_matrix` how to determine index and value of maximum of each row? 查找最大吸收值。 每组行索引的值,排列最大值 矩阵中的对角线值,每个索引的非对角线值,确定行列式 - Find Maximum abs. value for each group of row index, Arrange max. values diagonally in matrix, non diagonal values as per indexes,find determinant 在 3 维图像上覆盖具有透明度的蒙版 - Overlay a mask with transparency on a 3-dimentional image 查找python数据帧中每列的最大值之前的索引 - find index of a value before the maximum for each column in python dataframe 将矩阵每一行的最大值相加 - Add up the maximum value in each row of a matrix 如何在张量流中获取矩阵中每一行的最大值和次级最大值? - How to get the maximum and secondary maximum values of each row in a matrix in tensorflow? python稀疏矩阵获取最大值和索引 - python sparse matrix get maximum values and index 如何在每个索引的3个或更多数组上找到最大值 - How to find the maximum on 3 or more arrays at each index 通过numpy中行的最大值查找列索引 - Find the column index by the maximum value of row in numpy python中矩阵的最大列和行总和 - Maximum column and row sum of matrix in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM