在矩阵中查找最小值的索引(Find index of min value in a matrix)

编程入门 行业动态 更新时间:2024-10-24 04:38:03
矩阵中查找最小值的索引(Find index of min value in a matrix)

我有一个2-Dim数组,包含给定拟合的剩余平方和(这里不重要)。

RSS[i,j] = np.sum((spectrum_theo - sp_exp_int) ** 2)

我想找到矩阵元素的最小值和它在矩阵中的位置(i,j)。 找到最小元素就可以了:

RSS_min = RSS[RSS != 0].min()

但对于索引,我试过:

ij_min = np.where(RSS == RSS_min)

这给了我:

ij_min = (array([3]), array([20]))

我想取而代之的是:

ij_min =(3,20)

如果我尝试:

ij_min = RSS.argmin()

我获得:

ij_min = 0,

这是一个错误的结果。

它是否存在于Scipy或其他地方的函数,可以做到吗? 我在网上搜索过,但我发现只有1-Dim阵列的答案,而不是2-D-Dim。

谢谢!

I've a 2-Dim array containing the residual sum of squares of a given fit (unimportant here).

RSS[i,j] = np.sum((spectrum_theo - sp_exp_int) ** 2)

I would like to find the matrix element with the minimum value AND its position (i,j) in the matrix. Find the minimum element is OK:

RSS_min = RSS[RSS != 0].min()

but for the index, I've tried:

ij_min = np.where(RSS == RSS_min)

which gives me:

ij_min = (array([3]), array([20]))

I would like to obtain instead:

ij_min = (3,20)

If I try :

ij_min = RSS.argmin()

I obtain:

ij_min = 0,

which is a wrong result.

Does it exist a function, in Scipy or elsewhere, that can do it? I've searched on the web, but I've found answers leading only with 1-Dim arrays, not 2- or N-Dim.

Thanks!

最满意答案

基于您现在所拥有的最简单的修复方法就是从数组中提取元素作为最后一步:

# ij_min = (array([3]), array([20])) ij_min = np.where(RSS == RSS_min) ij_min = tuple([i.item() for i in ij_min])

The easiest fix based on what you have right now would just be to extract the elements from the array as a final step:

# ij_min = (array([3]), array([20])) ij_min = np.where(RSS == RSS_min) ij_min = tuple([i.item() for i in ij_min])

更多推荐

本文发布于:2023-07-14 20:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1107291.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:矩阵   索引   最小值   Find   min

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!