Error using interp1 on non-uniform vectors

I'm using interp1 to interpolate some spatially non-uniform velocity data to a uniform spatial vector. My data looks something like this, with leading and trailing NaNs in the 2nd column (velocity) and real numbers in the 1st column (x-position):
data = [-5.13 NaN; -5.07 1.45; ...; 4.27 1.67; 5.03 NaN];
other_data = [-4.32 NaN; -4.14 1.57; ...; 4.25 1.89; 4.56 NaN];
I have several of these arrays of varied length with varied start and stop positions. My code grabs the smallest/largest x-values and uses them to build the grid for interpolation. For simplicity we'll say data contains the largest/smallest x values:
x_interp = [min(data(:,1)): 0.02 :max(data(:,1))].';
And the interp1 call looks like, where the first column of final_data is the x-values from x_interp:
final_data(:,i) = interp1(other_data(:,1),other_data(:,2),x_interp);
This call worked on one set of data, but not another and I'm having a hard time picking out what the difference in the data is. When I do this, I'm getting the following error message:
Error using griddedInterpolant
The grid vectors do not define a grid of points that match the given values.
Error in interp1 (line 166)
F = griddedInterpolant(X,V(:,1),method);
Any suggestions/ideas?

1 Comment

W/O a set that doesn't work as a minimum, hard to tell, but -- have you accounted for the possible case of a zero-length vector or other boundary conditions? Not sure on the error itself; never gotten that particular one that I recall so don't have an experience base on which to draw.
You might want to look at John d'Errico's in_paintnan() (or similar spelling if not exact) in File Exchange. It has many options for handling NaN in vectors one of which might make your task simpler following its use...

Sign in to comment.

 Accepted Answer

If the NaN values were at the leading and trailing (I assume at both ends) of your position-velocity matrix, and you are using the velocities to create your interpolation vector, you are likely extrapolating (or at least interp1 thinks so). See if changing the interp1 call to:
final_data(:,i) = interp1(other_data(:,1),other_data(:,2),x_interp, 'linear', 'extrap');
changes its behaviour. (You might want a different method than 'linear'. There are several to choose from, but if you extrapolate, you must specify a method.) Also, interp1 may not like the NaN values, so deleting the entries that have them may help get your code to work.
If you are still having problems with those changes, it could help if you uploaded a representative subset of the data that worked and the data that failed, along with the relevant part of the code you used in both situations. Did all of them have some NaN values in the velocity column?

4 Comments

Thank you for your response. I did indeed try the 'extrap' method. interp1 seems to be ok with NaN values. I seem to have tracked down the problem after going through each dataset one by one.
The interp1 function is OK with NaN values unless a point you want to interpolate is the same as the x value with a y=NaN value or two consecutive y=NaN values and your interpolation is between them, the reason I suggested deleting those rows. At least that’s been my experience with it.
The repeated x value should have thrown an error saying that the x values have to be monotonically increasing (no repeated or decreasing x values). The solution to the problem of a number of consecutive equal x values is to add to them a vector of small values, ([1:n]*1E-10), with n the number of consecutive values, to create a monotonically increasing series.
Sorry, I should have been clearer. It appears I had a bad reference somewhere in my code because the previous error message I indicated has not shown up again. Now, it is throwing the non-monotonic error, but at least I understand why that error is showing up. Thank you for your suggestions, I will try them out.
My pleasure!
Consider using the unique function (with all three outputs) to find the repeating values.
For example:
A = [1:5 5:10 10 10:15];
[Xu, ia, ic] = unique(A); % Find Unique Elements
Xd = diff(ia); % Differences to Detect Repeats
Xri = find(diff(ia)>1); % Indices of First Values in Repeat Sequence
Xrn = Xd(Xri); % Number of Repeats in Each Sequence

Sign in to comment.

More Answers (1)

Derek
Derek on 19 Aug 2014
I seem to have tracked down the problem. griddedInterpolant did not like the fact that one of the datasets had a repeated x value. That is, two of the same x values with different velocity values. Thank you for your responses.

Categories

Find more on Interpolation in Help Center and File Exchange

Asked:

on 18 Aug 2014

Edited:

on 19 Aug 2014

Community Treasure Hunt

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

Start Hunting!