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?
Asked
Active
Viewed 7,962 times
1

Karlo
- 1,630
- 4
- 33
- 57
1 Answers
1
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

Dan
- 45,079
- 17
- 88
- 157
-
1Shouldn't it be `vq(idxMin) = v(1)` and `vq(idxMax) = v(end)` instead of `= max(x)` and `= min(x)` ? – CitizenInsane Feb 15 '16 at 10:37
-
Indeed. That's how I do it. – Karlo Feb 15 '16 at 10:46
-
2@CitizenInsane I'm not sure if that's right either because `x` doesn't have to be monotonically increasing... so you might not end up clipping at `v(1)` for the minimum. I've made a correction, not 100% of it either though. – Dan Feb 15 '16 at 10:53
-
Yes it's more generic this way, thought `interp1` was requiring for monotonic `x`, but it's `interp1q` which requires this. – CitizenInsane Feb 15 '16 at 11:01