In matlab function interp1- does the order of the new positions, for which the function values are interpolated, matter?

时间:2015-07-08 15:39:00

标签: matlab

Regarding matalb function yi=interp1(x,Y,xi,method) where x are the positions of the known values of Y, and xi are the new positions, whose Y values need to be interpolated - does the xi values need to be ordered for the function interp1 to work properly?

1 个答案:

答案 0 :(得分:1)

No, the query points xq do not have to be sorted.

Consider the following example:

x = 0:pi/4:2*pi;
v = sin(x);

xq_sorted = 0:pi/16:2*pi;

% shuffle the query points randomly
shuffle = randperm(length(xq_sorted));
xq_shuffled=xq_sorted(shuffle);

% interpolate both the sorted and the shuffled query points
vq_sorted = interp1(x,v,xq_sorted);
vq_shuffled = interp1(x,v,xq_shuffled);

% compare results
if any(vq_sorted(shuffle)~=vq_shuffled)
    disp('interpolation results do not match');
else
    disp('interpolation results match');
end

output

interpolation results match