-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlininterp1.m
34 lines (28 loc) · 936 Bytes
/
lininterp1.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
function v = lininterp1(X, V, x)
% linear interpolation, given set of X and V values, and an x query
% assumes X values are in strictly increasing order
%
% Differences from matlab built-in :
% much, much faster
% if coordinate is exactly on the spot, doesn't look at neighbors. e.g. interpolate([blah, blah2], [0, NaN], blah) returns 0 instead of NaN
% extends values off the ends instead of giving NaN
%
if length(X) ~= length(V), error('X and V sizes do not match'); end
pindex = find((x >= X), 1, 'last');
index = find((x <= X), 1, 'first');
if isempty(pindex)
warning('interpolating before beginning');
pindex = index;
slope = 0;
elseif isempty(index)
%warning('interpolating after end');
index = pindex;
slope = 0;
elseif pindex == index
slope = 0;
else
Xp = X(pindex);
slope = (x - Xp) / (X(index) - Xp);
end
v = V(pindex) * (1 - slope) + V(index) * slope;
end