如何使用 Matlab 的 interp1 进行线性插值和剪辑外推

How to use Matlab's interp1 with linear interpolation and clip extrapolation

从Matlab的interp1的文档看,插值和外推的方法应该是一样的。但是,我想用剪辑外推(保持极值)实现线性插值。这可以使用 interp1 函数吗?

看来您不能直接从 interp1 函数执行此操作:

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.

但我想自己实现起来并不难:

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