MATLAB: Find two minimum values not followed by each other.

MATLABmink

I am using mink(A,2) to find the two smallest values, but I need to find the two smallest values not followed by each other. How can this be done?
To illustrate: A(1; 1.2; 1.2; 0.7; 0.6; 0.61; 1; 1; 1.2;0.65) I want to find 0.6 and 0.65 and not 0.6 and 0.61.

Best Answer

Try this. It also preserve the order of minumum values
A = [0.65;1.2;1.2;0.7;0.6;0.61;1;1;1.2;1];
[vals,idx] = mink(A,3);
if diff(idx(1:2))==1
I = sort(idx([1 3]));
min_vals = A(I);
else
I = sort(idx([1 3]));
min_vals = A(I);
end
Result:
I =
1
5
min_vals =
0.6500
0.6000