About interp1.m (1D interpolation) function?
3 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
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
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.
更多回答(4 个)
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!
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
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
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;%
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Interpolation of 2-D Selections in 3-D Grids 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!