Matlab:矩阵的最小值 [英] Matlab : Minimum of matrix
问题描述
我需要找到整个矩阵的最小值,它是坐标". 在类似
I need to find the minimum of an entire matrix and it's 'coordinates'. In a matrix like
matrix = 8 7 6 5
4 3 2 1
在(2,4)处最小值为1.
The minimum would be 1 at (2, 4).
推荐答案
这可以非常简单地通过使用 find
,您将在其中使用它的两个输出版本.因此,您要做的就是搜索矩阵中与矩阵最小值匹配的行和列位置.
This can very simply be done by using find
, where you would use the two output version of it. So what you want to do is search for those row and column locations in your matrix that match the minimum value in your matrix.
因此:
[row, col] = find(matrix == min(matrix(:)));
row
和col
将包含等于此最小值的matrix
的行和列位置.请注意,我必须通过执行matrix(:)
将矩阵展开为向量.之所以这样,是因为如果您要在矩阵上使用min
,则默认情况下它将为您提供沿每一列的最小值.因为要查找整个矩阵的最小值,所以可以将其转换为单个矢量,然后沿整个矢量找到最小值.
row
and col
would contain the row and column locations of matrix
that are equal to this minimum value. Note that I had to unroll the matrix into a vector by doing matrix(:)
. The reason why is because if you were to use min
on a matrix, it would by default give you the minimum along each column. Because you want to find the minimum over the entire matrix, you would convert this into a single vector, then find the minimum along the entire vector.
请注意,这将返回与最小值匹配的所有行和列位置,因此实际上会将row
和col
用作N x 1
列向量,其中N
是matrix
中等于最小值的元素总数.
Take note that this will return all row and column locations that match the minimum, so it would actually give row
and col
as N x 1
column vectors, where N
is the total amount of elements in matrix
that equal the minimum.
如果您只想一个匹配,只需将1作为第二个参数附加到find
:
If you only want one match, simply append a 1 as the second parameter to find
:
[row, col] = find(matrix == min(matrix(:)), 1);
这篇关于Matlab:矩阵的最小值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!