MATLAB: When I use INTERP1, why do I receive the error “The data abscissae should be distinct”

MATLAB

When I use INTERP1, why do I receive the following error:
The data abscissae should be distinct.

Best Answer

This error is due to the 'x' data given for INTERP1 repeating at one or more data points. INTERP1 will not be able to interpolate if there are multiple values of 'y' for every 'x'.
The workaround is to either obtain new data, or exclude the duplicate data points in all the repeated values of 'x'. You can do this using the UNIQUE function. The three-output syntax for UNIQUE will allow you to obtain a vector with distinct 'x' values and the corresponding 'y' values for those points.
As an example:
x = [1:10 5]; % x has a duplicate 5 in it
y = x.^2;
x55=interp1(x, y, 5.5) % Obtain the interpolated value at x=5.5 - this errors
[b,i,j]=unique(x); % Remove duplicates from x
x55=interp1(b, y(i), 5.5) % Obtain the interpolated value at x=5.5 - this succeeds
If the values in the 'y' vector corresponding to values in 'x' are different, you will need to choose a method by which to resolve this ambiguity. The example above using UNIQUE selects the 'y' value corresponding to the location of the last value in the 'x' vector with the nonunique value.