简体   繁体   English

使用“最小”功能时如何处理多个最小值

[英]How to deal with multiple minimum values when using “min” function

I am using matlab's "min" function to determine the index corresponding to the minimum value within a array (just a vector, actually)... All's well and good, except that I've found that when there are multiple values in the array that share the minimum value, the function [C, I] = min(A) returns only one of the indices. 我正在使用matlab的“最小”函数来确定与数组中的最小值相对应的索引(实际上只是一个矢量)...一切都很好,除了我发现数组中有多个值时共享最小值的函数[C,I] = min(A)仅返回其中一个索引。 This actually would not be an issue, except that the index it returns is not always the first (ie, smallest) index that has the minimum value. 实际上,这不是问题,只是它返回的索引并不总是第一个(即最小的)具有最小值的索引。 The documentation says that this should be the case (so, if entry #4 and entry #13 in an array have the same (minimum) value, it should return I = 4), but that's not what's happening. 该文档说应该是这种情况(因此,如果数组中的条目#4和条目#13具有相同(最小)值,则应返回I = 4),但事实并非如此。

Does anyone know how to have the min function return the smallest/lowest index for a shared minimum value within an array/vector? 有谁知道如何让min函数针对数组/向量内的共享最小值返回最小/最低索引? Relatedly, can anyone explain why the function is not behaving as it seemingly should? 与此相关的是,谁能解释为什么该功能表现不正常?

Thanks, 谢谢,

Ben Mooneyham 本·穆尼汉姆

As stated above, the values are then likely not the same. 如上所述,这些值可能不相同。 Consider 考虑

a = [1 2 3 4 2 4 3 1];
b = a;
b(1) = 1+eps; b(end) = 1-eps; % added a small error to the 1st and 8th element
[~,Ia] = min(a);
[~,Ib] = min(b);

where Ia is 1 and Ib would be 8. 其中Ia为1, Ib为8。

A solution is to round off your inputs: 一个解决方案是四舍五入您的输入:

f = 0.1;% rounding off to 1 decimal place
c = round(b/f)*f;
[~,Ic] = min(c);

where Ic will be 1, as expected. Ic等于1。

暂无
暂无

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

相关问题 如何找到最大值和分钟。 在数组中使用最小比较? - How to find max. and min. in array using minimum comparisons? 当每个键有多个值时,如何访问 map 和 Hash 到最小值? - How to access and map a Hash to minimum value when there are multiple values per key? 如何使用最小函数获得产品的最小值? - How to get the minimum value of a product using minimum function? 如何通过使用min_by比较多个块来获取数组的最小值? - How to get the minimum value of an array, based on comparing more than one block using min_by? 如何找到具有相同长度的多个 arrays 的最小值的数组 - How to find an array with the min values of multiple arrays with the same length 如何使用Javascript中的一个函数为多个变量分配不同的值 - How to assign multiple variables different values using one function in Javascript 使用numpy数组时如何处理itertools.product? - How to deal with itertools.product when using numpy array? 使用 min(a,b) 查找数组中最小值的索引 - Finding index of minimum value in array, USING min(a,b) JS Math方法的问题:函数正在删除多个最小/最大值(我认为?) - Problem with JS Math methods: function is removing multiple min/max values (I think?) 我想将数组从主方法传递给函数(最小值),以便可以在函数(最小值)中计算最小值 - I would like to pass an array from my main method to my function(min) so that the minimum can be calculated in the function(min)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM