简体   繁体   English

如何让MATLAB显示2D数组中最小值的索引?

[英]How to get MATLAB to display the index of the minimum value in a 2D array?

I'm trying to write a script in MATLAB that finds the location of the minimum value of a 2D array of numbers. 我正在尝试在MATLAB中编写一个脚本,找到二维数组的最小值的位置。 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. 我确定此阵列中只有1个最小值,因此在阵列中具有相同最小值的多个位置不是问题。 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. 我能找到数组的最小值 ,但在一个30×30阵列,我想知道哪些行和列最小值在不在。

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. 作为替代版本,将min组合以获得最小值并找到返回索引,如果您已经计算了最小值,则只需使用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)));

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: 您可以将矩阵重新整形为矢量,使用MIN找到最小值的索引,然后将此线性索引转换为矩阵索引:

>> 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

Look at the description of the min function. 看一下min函数的描述。 It can return the minimum value as well as the index. 它可以返回最小值以及索引。 For a two dimensional array, just call it twice. 对于二维数组,只需调用它两次。

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的解决方案可能在计算上更优雅(更快,需要更少的临时空间)

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是向量,“lowerBound”和“upperBound”是向量的边界,您需要在其中找到最大(或最小)值,然后使用此命令 -

[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. 这将返回“值”作为A(lowerBound)和A(uppedBound)和“Index”中的最小值或最大值,与“lowerBound”一样作为偏移量。 So to find the absolute index, you need to add "lowerBound" to the Index. 因此,要查找绝对索引,需要在索引中添加“lowerBound”。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM