[英]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.