About interp1.m (1D interpolation) function?
Show older comments
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
More Answers (4)
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
on 21 Aug 2012
Jürgen
on 20 Aug 2012
1 vote
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
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
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;%
Azzi Abdelmalek
on 20 Aug 2012
0 votes
i suggest that you replace one "2" by "2.01";
Alex Mrozack
on 6 Jan 2015
0 votes
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 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!