MATLAB: How to find the index of the closest value to some number in 1D array

arrayfindindexindexingMATLAB

How to find the index in 1D array that has closest value to some number ?
val =-1.03
val1 = 1.04
x = -10:0.009:10
ind1 = find (A==val) % will work if the val is exact match

Best Answer

Hint:
>> [ d, ix ] = min( abs( x-val ) );
>> x(ix-1:ix+1)
ans =
-1.0360 -1.0270 -1.0180
ix is the "index in 1D array that has closest value to" val
"if the val is exact match"   that's tricky with floating point numbers