interp1: The grid vectors are not strictly monotonic increasing

6 views (last 30 days)
Hi!
I want to interpolate a set of x-,y-values (2D-coordinates). My problem is the error in title.
I watched for a solution in web, but that didn't solve my problem. What I tried:
if
% My code:
xi = 0:0.1:length(x);
[x,index] = sort(x);
yi = interp1(x,y(index),xi);
% End of my code
end
But same Error...
PS: how do integrate the code here?
Greetings, Max

Accepted Answer

Matt J
Matt J on 5 Sep 2014
Edited: Matt J on 5 Sep 2014
The data you've plotted here suggests that you're really trying to interpolate a space curve, not a function. Consider this FEX file instead,
Alternatively, if you can write your curve in the parametric form x(t),y(t) you can interpolate both x and y with respect to t using the spline() command.

More Answers (2)

Matt J
Matt J on 5 Sep 2014
Edited: Matt J on 5 Sep 2014
Some of your x(k) are duplicates of each other. Remove the duplicates, e.g., using unique()
  2 Comments
Maximilian
Maximilian on 5 Sep 2014
Sorry i forgot, that this was already included with:
if true
% My Code after end:
end
n=1;
while (n < length(x)-1)
if( abs([x(n+1) - x(n)]) <= 0.00009) % 0.009
x(n) = [];
y(n) = [];
elseif( abs([y(n+1) - y(n)]) <= 0.00009)
y(n) = [];
x(n) = [];
elseif(x(n) == x(n+1))
x(n) = [];
y(n) = [];
elseif(y(n) == y(n+1))
x(n) = [];
y(n) = [];
else
n = n+1;
end
end
Matt J
Matt J on 5 Sep 2014
Edited: Matt J on 5 Sep 2014
The while loop won't do what you want because length(x) is changing throughout the loop. Therefore, the stopping condition (n < length(x)-1) as well as the value of n itself at any given point in the loop are meanginless. Use unique instead.

Sign in to comment.


Guillaume
Guillaume on 5 Sep 2014
Edited: Guillaume on 5 Sep 2014
My guess is that you have some values of x that repeat thus it's not strictly monotonic. If you're happy just discarding all but one of the y for the repeating x the following will work:
xi = 0:0.1:length(x);
[x, index] = unique(x); %sort and remove duplicates
yi = interp1(x, y(index), xi); %edited for typo in interp1 name
If you want something more complex for the duplicates (like mean) you'll have to say what.
  4 Comments
Maximilian
Maximilian on 5 Sep 2014
Edited: Matt J on 5 Sep 2014
Okay, with much points for interpolation, that works. But now I already have the problem, that the interpolated data (red graph) does not match very good to my data (blue graph).
Can I do it better?
Guillaume
Guillaume on 5 Sep 2014
Well, yes, interp1 won't work for this. It's not a function of x.

Sign in to comment.

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!