From the documentation of Matlab's interp1
, it seems that the method used for interpolation and extrapolation should be the same. However, I would like to implement a linear interpolation with clip extrapolation (hold extreme values). Is this possible using the interp1
function?
1
votes
1 Answers
1
votes
It looks like you can't do it directly from the interp1
function:
Extrapolation strategy, specified as the string, 'extrap', or a real scalar value.
- Specify 'extrap' when you want interp1 to evaluate points outside the domain using the same method it uses for interpolation.
- Specify a scalar value when you want interp1 to return a specific constant value for points outside the domain.
but I guess it's not too hard to implement yourself:
function vq = LinearInterpWithClipExtrap(x,v,xq)
vq = interp1(x,v,xq);
[XMax, idxVMax] = max(x);
[XMin, idxVMin] = min(x);
idxMax = xq > XMax;
idxMin = xq < XMin;
vq(idxMax) = v(idxVMax);
vq(idxMin) = v(idxVMin);
end