Use griddedinterpolant to interpolate over missing values
27 views (last 30 days)
Show older comments
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
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.
Accepted Answer
Walter Roberson
on 28 Feb 2017
You may also wish to examine the techniques used in https://www.mathworks.com/matlabcentral/fileexchange/4551-inpaint-nans
More Answers (2)
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
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!