Use griddedinterpolant to interpolate over missing values

28 views (last 30 days)
I would like to use griddedInterpolant to interpolate over datapoints that I need to discard from an N-dimensional matrix. Specifically, the undesirable values represent "spikes" in my data set that I have identified and need to remove. I thought that this would be very easy and fast to accomplish with griddedInterpolant, but I can't seem to figure out the syntax.
Here's an example. Let's say my data is in matrix A, which has dimensions [x y z]. Then I have another logical matrix isSpike of the same dimensions [x y z]. I thought I could simply type:
[X1, X2, X3] = ndgrid(1:x, 1:y, 1:z);
G = griddedInterpolant(X1(~isSpike), X2(~isSpike), X3(~isSpike), A(~isSpike), 'linear');
One could simply read this as "make a griddedInterpolant object centered on each non-spike element in my original dataset." Then, I thought I could simply get the interpolated data using:
B = G(X1, X2, X3);
Simple, right? However, I get an error when calling the griddedInterpolant command in this manner:
Error using griddedInterpolant
The number of input coordinate arrays does not equal the number of dimensions (NDIMS) of these arrays.
Could this be due to the behavior Matlab takes when one uses an N-dimensional logical indexing? For example, if I try:
test = X1(~isSpike);
test is returned as a vector having dimensions [1 (prod(x, y, z) - sum( isSpike ))]. In other words, it is transformed into a 1 dimensional vector array, and this cannot have the same dimensions as the original 3D matrix, since there are now fewer elements.
Regardless of the cause, does anyone have any suggestions on how to work around this?
  1 Comment
Stephen23
Stephen23 on 28 Feb 2017
The very first line of the griddedInterpolan documentation states that "Use griddedInterpolant to perform interpolation on a 1-D, 2-D, 3-D, or N-D Gridded Data set". Your data is not gridded (it has holes), therefore you cannot use griddedInterpolant.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 28 Feb 2017
  1 Comment
Thomas
Thomas on 28 Feb 2017
Thanks for this suggestion. I have tried this code and it works just fine. However, it is very slow. One dataset I tried this on took over 80 seconds to complete. Moreover, I need to resample the dimensions of my dataset during my workflow, and this can be done very rapidly and efficiently using griddedInterpolant. So this is like "two birds with one stone" as it would seriously speed up my code and improve readability if I could remove the bad data points using griddedInterpolant.

Sign in to comment.

More Answers (2)

Guillaume
Guillaume on 28 Feb 2017
The shape of your coordinate inputs is indeed an issue, but a gridded interpolant needs a full grid, one without any hole, so you cannot use it for your case anyway.
You can use a scatteredInterpolant instead, which would work fine with your current syntax, or as Walter suggests, the FEX inpaint_nans.
  3 Comments
Thomas
Thomas on 28 Feb 2017
It seems to work on 3D data as well, although it returns a 2D matrix in this case, and needs to be reshaped manually. It does not seem to work on N-dimensional data, though.

Sign in to comment.


Thomas
Thomas on 28 Feb 2017
Both of the suggestions by Walter and Guillaume worked very well for me. If I choose to use the exact syntax I suggested above, then scatteredInterpolant works perfectly. However, in my experience, it was prohibitively slow. The inpaintn function:
worked perfectly, but was also relatively slow for large datasets. However, it takes an n-dimensional array as input, which may be helpful. The inpaint_nans function:
worked the best for my application. It was extremely fast, only taking about 2 seconds to complete. And it gave comparable results to the other methods. I can even input a 3D matrix, although I receive a 2D matrix as output. But this can be easily reshaped back to the original dimensions.
My conclusion is that the best workflow is to remove the spikes first and then use griddedInterpolant later if I need to interpolate my data. This is only slightly less readable, but offers great speed advantages over the scatteredInterpolant method.

Categories

Find more on MATLAB in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!