【发布时间】:2021-08-26 16:59:11
【问题描述】:
我一般是 numpy 和 python 的新手,我希望找到每个 2D 子数组的最小值,给定一个 3D 数组。例如:
# construct an example 3D array
a = np.array([[5,4,1,5], [0,1,2,3], [3,2,8,1]]).astype(np.float32)
b = np.array([[3,2,9,3], [8,6,5,3], [6,7,2,8]]).astype(np.float32)
c = np.array([[9,7,6,5], [4,7,6,3], [1,2,3,4]]).astype(np.float32)
d = np.array([[5,4,9,2], [4,2,6,1], [7,5,9,1]]).astype(np.float32)
e = np.array([[4,5,2,9], [7,1,5,8], [0,2,6,4]]).astype(np.float32)
a = np.insert(a, 0, [np.inf]*len(a), axis=1)
b = np.insert(b, 1, [np.inf]*len(b), axis=1)
c = np.insert(c, 2, [np.inf]*len(c), axis=1)
d = np.insert(d, 3, [np.inf]*len(d), axis=1)
e = np.insert(e, 4, [np.inf]*len(e), axis=1)
arr = np.swapaxes(np.dstack((a,b,c,d,e)), 1, 2)
print(arr)
给出这个结果: 3D Matrix
我正在寻找的结果是每个二维数组中最小元素的索引,类似于:
[[0, 0, 3], # corresponding to the coordinates of element with value 1 in the first 2D array
[1, 0, 1], # corresponding to the coordinates of element with value 0 in the second 2D array
[2, 4, 0]] # corresponding to the coordinates of element with value 0 in the third 2D array
或类似的东西。我计划使用索引来获取该值,然后用无限值替换该二维子数组中的列和行,以找到不在同一行/列中的下一个最小值。
感谢您的帮助,谢谢!
【问题讨论】:
标签: python arrays numpy multidimensional-array sub-array