# How to interpolate to the nearest value

145 views (last 30 days)
Ede gerlderlands on 10 Jun 2013
HI
I have vector, v=(x,Y) and I want to find the nearest value of 'x' for C=(1,yi) rather than the interpolated values of xi's .I can find the interpolated value but taking the nearest values of x is difficult for me. Can you help me with this?

Kelly Kearney on 10 Jun 2013
If you look at the documentation of interp1, you'll see that nearest neighbor interpolation is an option:
interp1(x,y,xi, 'nearest')
Ede gerlderlands on 10 Jun 2013
Thank you.

Pourya Alinezhad on 10 Jun 2013
here is the answer to your question. :) t = 1900:10:1990; >> p = [75.995 91.972 105.711 123.203 131.669... 150.697 179.323 210.212 256.505 270.633];
>> interp1(t,p,1975) ans = 233.3585
Pourya Alinezhad on 10 Jun 2013
so you can do in an other way : x=[ 1 8 35 ...] y=[ 23 55 3 ...] for example we want nearest value to x=8;
a=find(x,8);
min=inf;
num_min=[];
for i=1:length(x)
if abs(y(a)-y(i))<min
min=abs(y(a)-y(i));
num_min=i;
end
end