lookup.m (623B)
1 function yval=lookup(x,y,xval); 2 3 % function yhat=lookup(x,y); 4 % 5 % Given two series x and y, think of y=f(x). Using linear 6 % interpolation, compute yval=f(xval). 7 % 8 % Can handle vectors of xval... 9 10 T=length(xval); 11 yval=zeros(T,1); 12 for i=1:T; 13 14 % First see if that value exists 15 ival=find(x==xval(i)); 16 if ~isempty(ival); 17 yval(i)=y(ival); 18 else; 19 % Otherwise, we have to interpolate 20 iless=find(x<xval(i)); 21 imore=find(x>xval(i)); 22 iless=iless(end); % Last below 23 imore=imore(1); % First above 24 beta=(y(imore)-y(iless))/(x(imore)-x(iless)); 25 yval(i)=y(iless)+beta*(xval(i)-x(iless)); 26 end; 27 28 end;