简体   繁体   中英

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

Does anyone know how to have the min function return the smallest/lowest index for a shared minimum value within an array/vector? 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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