About interp1.m (1D interpolation) function?

4 views (last 30 days)
Hello, In function interp1.m, is it mandatory to have distinct values of X. As of now matlab gives an error if we have non-distinct values of X. But can this restriction be relaxed so that at most one pair of identical values of X is allowed?
example:
interp1([1 2 2 3 4],[1 2 3 4 5], 3.5); ==>> This gives an error saying 'X should have distinct values'.
My question is can this restriction be relaxed as there is only one pair (2,2) of identical values of X? (discontinuous interpolation?)
Thank you in advance
With regards, Vivek

Accepted Answer

Jan
Jan on 20 Aug 2012
A direct answer: No. INTERP1 requires distinct X-values. The idea of adding EPS or 0.01 to one of the values is not stable, because the result of the interpolation critically depends on the choice, if the first or second element is moved. It would be smarter and numerically stable to claculate the mean of the Y-values of repeated X-values.
  3 Comments
Jan
Jan on 20 Aug 2012
Of course it is, Azzi. And is should be. Imagine these values:
x = [1,2,2,3]; y = [4,3,5,6];
Now obtain the value at 2. When you subtract eps from the 1st 2, you get yi=5, when you add eps to the 2nd 2 you get yi=3. Therefore the strategy of adding an negligible value to the nun-unique x-values is not stable - this means that the results critically depend on tiny variations of the inputs.
In opposite to this, the transformation of x and y to:
xx = [1,2,3]; yy = [4,4,6];
be building the mean of the y-values for non-unique x-values allows for a reproducible interpolation.
Vivek Dogra
Vivek Dogra on 21 Aug 2012
Thank you all of you for your reply. Actually i wanted to know whether it is correct to allow identical values of X at all.
Because in Octave, interp1.m allows to have at most one pair of identical values in X.
% code
X = [1 2 2 3];
Y = [1 2 3 4];
interp1(X,Y,2); // is a valid input in octave.
In octave they define something called discontinuous interpolant. Their documentation says, in case of identical values, if X is increasing then the interpolation is right continuous. So for the above case the answer will be 3.
% code
X = [3 2 2 1];
Y = [4 3 2 1];
interp1(X,Y,2) //in octave gives '2'.
I think this concept is flawed. I just want to have your opinions on the same.
With regards, Vivek

Sign in to comment.

More Answers (4)

José-Luis
José-Luis on 20 Aug 2012
Edited: José-Luis on 20 Aug 2012
Interp1 draws segments between succesive points. If there are two points with the same ordinate, the problem is which one to choose? It's a decision matlab can't do for you. Depending on your problem, you can select to use the minimum, the maximum, the median, etc... For that you would have to write you own code. Say i wanted the mean of the repeated values:
a = [1 2 2 3 4]; b = [1 2 3 4 5];
data = [a' a' b'];
data(:,1) = data(:,1) - data(1) + 1;
x = accumarray(data(:,1),data(:,2),[],@min); %unique would work here as well
y = accumarray(data(:,1),data(:,3),[],@mean);
And now you can do your interpolation, with the mean of the repeated values:
interp1(x,y,valueToInterp);
Cheers!
  1 Comment
Vivek Dogra
Vivek Dogra on 21 Aug 2012
I totally agree with you. Actually i wanted to know whether it is correct to allow identical values of X at all.
Because in Octave, interp1.m allows to have at most one pair of identical values in X.
% code
X = [1 2 2 3];
Y = [1 2 3 4];
interp1(X,Y,2); // is a valid input in octave.
In octave they define something called discontinuous interpolant. Their documentation says, in case of identical values, if X is increasing then the interpolation is right continuous. So for the above case the answer will be 3.
% code
X = [3 2 2 1];
Y = [4 3 2 1];
interp1(X,Y,2) //in octave gives '2'.
I think this concept is flawed. I just want to have your opinions on the same.
Cheers!

Sign in to comment.


Jürgen
Jürgen on 20 Aug 2012
Hi, just a suggestion, I had a similar issue with a large data set of measurement where indeed you can have (x,y) and (x,y+dy) as measured values due measurement error or depingding on the nature of your measurement.
A solution can be to use the regression line to estimate yi for a value xi do not if it works for your problem of course,
regards,J
  2 Comments
Jan
Jan on 20 Aug 2012
Exactly. My suggestion to build the mean for identical X-values is a cheap variation of this method. Using a polynomial using a surrounding interval, which size is determined by physical reasons, is a much better idea.
Jürgen
Jürgen on 20 Aug 2012
I do it like this:
X1="known X values" Y="known Y values" X = [ones(size(X1)) X1]; A=X\Y; eqution of the line is then :
y = A(1)+A(2)*x;%
or yi= A(1)+A(2)*xi;%

Sign in to comment.


Azzi Abdelmalek
Azzi Abdelmalek on 20 Aug 2012
i suggest that you replace one "2" by "2.01";

Alex Mrozack
Alex Mrozack on 6 Jan 2015
I came across this chain because I was interested in allowing interp1 to interpolate about non-distinct values of X. There are times where this should be officially suppported, and adding epsilon is the correct thing to do. This is when the Y values are sorted and either monotonically increasing or decreasing. In this case, the true value of X likely is x(i)+eps for x(i+1)=x(i). This is likely to happen in cases of empirical estimation of ROCs, when the number of true detections might be low.
For non-monotonic data the aforementioned workarounds are the way to go.

Categories

Find more on Interpolation of 2-D Selections in 3-D Grids in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!