如何使MATLAB在2D数组中显示最小值的索引? [英] How to get MATLAB to display the index of the minimum value in a 2D array?

查看:119
本文介绍了如何使MATLAB在2D数组中显示最小值的索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在MATLAB中编写一个脚本,该脚本查找2D数字数组的最小值的位置.我确定此数组中只有1个最小值,因此在数组中具有多个具有相同最小值的位置不是问题.我可以找到数组的最小,但是在30x30的数组中,我想知道最小值位于哪个行和列中.

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.

推荐答案

作为替代版本,将min组合以获取最小值并查找以返回索引,如果您已经计算出最小值,则只需使用find. /p>

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.

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

这篇关于如何使MATLAB在2D数组中显示最小值的索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆