Use griddedinterpolant to interpolate over missing values

조회 수: 27 (최근 30일)
Thomas
Thomas 2017년 2월 28일
답변: Thomas 2017년 2월 28일
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
Stephen23
Stephen23 2017년 2월 28일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2017년 2월 28일
  댓글 수: 1
Thomas
Thomas 2017년 2월 28일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Guillaume
Guillaume 2017년 2월 28일
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
Walter Roberson
Walter Roberson 2017년 2월 28일
inpaint_nans is for 2D data.
Thomas
Thomas 2017년 2월 28일
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.

댓글을 달려면 로그인하십시오.


Thomas
Thomas 2017년 2월 28일
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.

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by