interp1 没有 return 预期结果

interp1 does not return expected results

我正在尝试对向量的值进行插值,但我似乎无法理解如何正确使用 interp1.m。

这就是我所期待的:

a=[1 0 2 0 3 0 4];
//Use of interp1.m
Output=[1 1.5 2 2.5 3 3.5 4];

a=[1 0 0 2 0 0 3 0 0 4];
//Use of interp1.m
Output=[1 1.32 1.65 2 2.31 2.64 3 3.3 3.63 4];

这就是我认为你打算使用的方式 interp1:

a=[1 2 3 4];
N=7; % # of points to interpolate between a(1)=1 and a(end)=4 
xi=linspace(a(1),a(end),N); % the new intep x-grid
ai=interp1(1:numel(a),a,xi)

附带说明一下,如果您只想在任意两个值之间使用线性间距,只需使用 linspace,例如:

 linspace(1,4,10)


ans =

1.0000    1.3333    1.6667    2.0000    2.3333    2.6667    3.0000    3.3333    3.6667    4.0000

零是您输入的实际值,如果您对它们进行插值,您 "force" 结果将通过它们...

假设您总是想填充向量的零值:

a = [3 0 6 0 5 0 4]

mask = logical(a);
nvec = 1:numel(a);
a(~mask) = interp1(nvec(mask),a(mask),nvec(~mask))

a =

    3.0000    4.5000    6.0000    5.5000    5.0000    4.5000    4.0000

假设您想要拉伸您的向量某个因子:

a = [3 6 5 4]
stretchfactor = 2;
a = interp1((1:numel(a))*stretchfactor - 1, a, 1:numel(a)*stretchfactor - 1)

a =

    3.0000    4.5000    6.0000    5.5000    5.0000    4.5000    4.0000