如何用MATLAB来显示二维数组中最小值的索引?

时间:2023-02-07 20:08:15

I'm trying to write a script in MATLAB that finds the location of the minimum value of a 2D array of numbers. I am certain there is only 1 minimum in this array, so having multiple locations in the array with the same minimum value is not an issue. I can find the minimum value of the array, but in a 30x30 array, I would like to know which row and column that minimum value is in.

我试着在MATLAB中编写一个脚本,找到一个二维数组的最小值的位置。我确信在这个数组中只有一个最小值,所以在数组中具有相同最小值的多个位置并不是问题。我可以找到数组的最小值,但是在30x30数组中,我想知道最小值在哪个行和列中。

5 个解决方案

#1


28  

As an alternative version, combine min to get the minimum value and find to return the index, if you've already calculated the minimum then just use find.

作为另一种版本,结合最小值获得最小值并找到返回索引,如果您已经计算了最小值,那么就使用find。

>> a=magic(30);
>> [r,c]=find(a==min(min(a)))

r =
     1
c =
     8

Or depending on how you want to use the location information you may want to define it with a logical array instead, in which case logical addressing can be used to give you a truth table.

或者,根据您想要使用位置信息的方式,您可能想用一个逻辑数组来定义它,在这种情况下,逻辑寻址可以用来给您一个真值表。

>> a=magic(30);
>> locn=(a==min(min(a)));

#2


13  

You could reshape the matrix to a vector, find the index of the minimum using MIN and then convert this linear index into a matrix index:

你可以将矩阵变换成一个向量,用最小值找到最小值,然后将这个线性指数转换成矩阵指数:

>> x = randi(5, 5)

x =

     5     4     4     2     4
     4     2     4     5     5
     3     1     3     4     3
     3     4     2     5     1
     2     4     5     3     5

>> [value, index] = min(reshape(x, numel(x), 1));
>> [i,j] = ind2sub(size(x), index)

i =

     3


j =

     2

#3


7  

Look at the description of the min function. It can return the minimum value as well as the index. For a two dimensional array, just call it twice.

看一下min函数的描述。它可以返回最小值和索引。对于二维数组,只需调用两次。

A = rand(30); % some matrix
[minColVal, minColIdx] = min(A);
[minRowVal, minRowIdx] = min(minColVal);

minVal = minRowVal;
minValIdx = [minColIdx(minRowIdx), minRowIdx];

Edit: @b3's solution is probably computationally more elegant (faster and needs less temporary space)

编辑:@b3的解决方案在计算上可能更优雅(更快,需要更少的临时空间)

#4


0  

To find min or max in a subset of a vector - If A is a vector and "lowerBound" and "upperBound" are the bounds of the vector among which you need to find the max (or min) value, then use this command -

要在向量的子集中找到最小值或最大值,如果a是向量,“下界”和“上界”是向量的边界,你需要找到最大值(或最小值)值,然后使用这个命令。

[Value,Index]=min(A(lowerBound:upperBound));

This returns "Value" as the min or max value among A(lowerBound) and A(uppedBound) and "Index" as with "lowerBound" as the offset. So to find the absolute index, you need to add "lowerBound" to the Index.

这将返回“值”作为一个(下界)和A(uppedBound)的最小值或最大值,以及与“下界”作为偏移量的“索引”。因此,要找到绝对索引,需要在索引中添加“下界”。

#5


0  

An alternate solution using an inline function will work.

使用内联函数的替代解决方案将起作用。

    >> min_index = @(matrix) find(matrix == min(reshape(matrix, [1,numel(matrix)])));

    >> a=magic(30);
    >> [r,c]=min_index(a)

    r =
         1

    c =
         8

#1


28  

As an alternative version, combine min to get the minimum value and find to return the index, if you've already calculated the minimum then just use find.

作为另一种版本,结合最小值获得最小值并找到返回索引,如果您已经计算了最小值,那么就使用find。

>> a=magic(30);
>> [r,c]=find(a==min(min(a)))

r =
     1
c =
     8

Or depending on how you want to use the location information you may want to define it with a logical array instead, in which case logical addressing can be used to give you a truth table.

或者,根据您想要使用位置信息的方式,您可能想用一个逻辑数组来定义它,在这种情况下,逻辑寻址可以用来给您一个真值表。

>> a=magic(30);
>> locn=(a==min(min(a)));

#2


13  

You could reshape the matrix to a vector, find the index of the minimum using MIN and then convert this linear index into a matrix index:

你可以将矩阵变换成一个向量,用最小值找到最小值,然后将这个线性指数转换成矩阵指数:

>> x = randi(5, 5)

x =

     5     4     4     2     4
     4     2     4     5     5
     3     1     3     4     3
     3     4     2     5     1
     2     4     5     3     5

>> [value, index] = min(reshape(x, numel(x), 1));
>> [i,j] = ind2sub(size(x), index)

i =

     3


j =

     2

#3


7  

Look at the description of the min function. It can return the minimum value as well as the index. For a two dimensional array, just call it twice.

看一下min函数的描述。它可以返回最小值和索引。对于二维数组,只需调用两次。

A = rand(30); % some matrix
[minColVal, minColIdx] = min(A);
[minRowVal, minRowIdx] = min(minColVal);

minVal = minRowVal;
minValIdx = [minColIdx(minRowIdx), minRowIdx];

Edit: @b3's solution is probably computationally more elegant (faster and needs less temporary space)

编辑:@b3的解决方案在计算上可能更优雅(更快,需要更少的临时空间)

#4


0  

To find min or max in a subset of a vector - If A is a vector and "lowerBound" and "upperBound" are the bounds of the vector among which you need to find the max (or min) value, then use this command -

要在向量的子集中找到最小值或最大值,如果a是向量,“下界”和“上界”是向量的边界,你需要找到最大值(或最小值)值,然后使用这个命令。

[Value,Index]=min(A(lowerBound:upperBound));

This returns "Value" as the min or max value among A(lowerBound) and A(uppedBound) and "Index" as with "lowerBound" as the offset. So to find the absolute index, you need to add "lowerBound" to the Index.

这将返回“值”作为一个(下界)和A(uppedBound)的最小值或最大值,以及与“下界”作为偏移量的“索引”。因此,要找到绝对索引,需要在索引中添加“下界”。

#5


0  

An alternate solution using an inline function will work.

使用内联函数的替代解决方案将起作用。

    >> min_index = @(matrix) find(matrix == min(reshape(matrix, [1,numel(matrix)])));

    >> a=magic(30);
    >> [r,c]=min_index(a)

    r =
         1

    c =
         8