About interp1.m (1D interpolation) function?

조회 수: 4 (최근 30일)
Vivek Dogra
Vivek Dogra 2012년 8월 20일
답변: Alex Mrozack 2015년 1월 6일
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

채택된 답변

Jan
Jan 2012년 8월 20일
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
Jan
Jan 2012년 8월 20일
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 2012년 8월 21일
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

댓글을 달려면 로그인하십시오.

추가 답변 (4개)

José-Luis
José-Luis 2012년 8월 20일
편집: José-Luis 2012년 8월 20일
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
Vivek Dogra
Vivek Dogra 2012년 8월 21일
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!

댓글을 달려면 로그인하십시오.


Jürgen
Jürgen 2012년 8월 20일
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
Jan
Jan 2012년 8월 20일
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 2012년 8월 20일
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;%

댓글을 달려면 로그인하십시오.


Azzi Abdelmalek
Azzi Abdelmalek 2012년 8월 20일
i suggest that you replace one "2" by "2.01";

Alex Mrozack
Alex Mrozack 2015년 1월 6일
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.

카테고리

Help CenterFile Exchange에서 Data Extraction에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by