MATLAB: How to find the location of approximately equal value in vector

finding location

I having Z =10.7000 and ACF(auto correlation function)
ACF =
Columns 1 through 6
0 -1.0000 -1.8478 -2.2678 -2.0719 -1.2071
Columns 7 through 12
0.2242 1.9749 3.6955 5.0000 5.5433 5.0962
Columns 13 through 18
3.6027 1.2071 -1.7549 -4.8033 -7.3910 -9.0000
Columns 19 through 24
-9.2388 -7.9246 -5.1334 -1.2071 3.2856 7.6317
Columns 25 through 30
11.0866 13.0000 12.9343 10.7530 6.6641 1.2071
here i want to find out approximately equal value of Z in ACF and also location of the ACF value. Here the Z value is approximately equal to 10.7530 of ACF and it present at location of ACF(28). Help me to write the code for find approximately equal value of Z in ACF and its location(28).

Best Answer

You should calculate the absolute differences, and then use min to get the closest value:
>> Z = 10.7000;
>> ACF = [0,-1.0000,-1.8478,-2.2678,-2.0719,-1.2071,0.2242,1.9749,3.6955,5.0000,5.5433,5.0962,3.6027,1.2071,-1.7549,-4.8033,-7.3910,-9.0000,-9.2388,-7.9246,-5.1334,-1.2071,3.2856,7.6317,11.0866,13.0000,12.9343,10.7530,6.6641,1.2071]
>> [~,idx] = min(abs(Z-ACF))
idx =
28
>> out = ACF(idx)
out = 10.753