How to interpolate to the nearest value

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
Kelly Kearney 2013년 6월 10일

1 개 추천

If you look at the documentation of interp1, you'll see that nearest neighbor interpolation is an option:
interp1(x,y,xi, 'nearest')

추가 답변 (1개)

Pourya Alinezhad
Pourya Alinezhad 2013년 6월 10일

0 개 추천

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

댓글 수: 4

Ede gerlderlands
Ede gerlderlands 2013년 6월 10일
yes, but what I want is the nearest value not the interpolated one. In the example you give me the nearest value to 233.358 is ' 210.212' and I want this value. Thanks
Pourya Alinezhad
Pourya Alinezhad 2013년 6월 10일
so you must write some line of codes .at first interpolate the value like that aforesaid.then subtract the result from upper and lower values in the vector.the smaller answer of subtraction is the nearest.
Ede gerlderlands
Ede gerlderlands 2013년 6월 10일
yes, am new to matlab and find it difficult
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

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Interpolation에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by