Clear Filters
Clear Filters

Interpolation of missing matrix element

12 views (last 30 days)
Hello, I am trying to interpolate some missing data of my 3x3 matrix,
M = [43.13 0 42.88;0 39.32 0;41.81 0 43.27];
where 0s represent the missing value that I would like to interpolate to fill it in.
I read through interp1-3 command but not so sure on what to do to find a missing values
Please help!

Accepted Answer

KSSV
KSSV on 1 Nov 2022
M = [43.13 0 42.88;0 39.32 0;41.81 0 43.27];
M(M==0) = NaN
M = 3×3
43.1300 NaN 42.8800 NaN 39.3200 NaN 41.8100 NaN 43.2700
M = fillmissing(M,'linear')
M = 3×3
43.1300 NaN 42.8800 42.4700 39.3200 43.0750 41.8100 NaN 43.2700
M = fillmissing(M','linear')'
M = 3×3
43.1300 43.0050 42.8800 42.4700 39.3200 43.0750 41.8100 42.5400 43.2700
  1 Comment
DGM
DGM on 1 Nov 2022
Edited: DGM on 1 Nov 2022
Fillmissing() only interpolates/extrapolates columnwise. Since the center column has only a single value, extrapolation fails. While transposing and filling again will clean up the remaining holes, it still means that the center pixel has no influence over any of the filled values.
M = [1 0 1; 0 0 0; 0 1E6 0; 1 0 1];
M(M==0) = NaN;
M = fillmissing(M,'linear')
M = 4×3
1 NaN 1 1 NaN 1 1 1000000 1 1 NaN 1
M = fillmissing(M','linear')'
M = 4×3
1 1 1 1 1 1 1 1000000 1 1 1 1
If there were enough data in the center column to support the default extrapolation behavior, the results would be wildly different.
M = [1 0 1; 0 1E6 0; 0 1E6 0; 1 0 1];
M(M==0) = NaN;
M = fillmissing(M,'linear')
M = 4×3
1 1000000 1 1 1000000 1 1 1000000 1 1 1000000 1
M = fillmissing(M','linear')'
M = 4×3
1 1000000 1 1 1000000 1 1 1000000 1 1 1000000 1
Using fillmissing() seems like it would be valid for columnwise organized data, but if that were the case, would it still make sense to interpolate across columns?

Sign in to comment.

More Answers (1)

DGM
DGM on 1 Nov 2022
I'm sure there are multiple ways, and interpolation may depend on whether this is 2D data or a bunch of independent rows of data. Here are a few things.
I'm not familiar with using fillmissing(), but using it like this doesn't seem right. It fills the holes, but the center pixel has no influence over filling its neighbors.
% above solution
M = [43.13 0 42.88;0 39.32 0;41.81 0 43.27];
M(M==0) = NaN;
M = fillmissing(M,'linear');
M = fillmissing(M','linear')';
imshow(M,[38 44])
If you have IPT, you can use regionfill()
% inpaint with IPT regionfill()
M = [43.13 0 42.88;0 39.32 0;41.81 0 43.27];
mask = M==0;
M = regionfill(M,mask);
imshow(M,[38 44])
If you don't have IPT, you could use John's inpainting tool from the FEX:
% inpaint with inpaint_nans()
M = [43.13 0 42.88;0 39.32 0;41.81 0 43.27];
M(M==0) = NaN;
M = inpaint_nans(M);
imshow(M,[38 44])

Categories

Find more on Interpolation in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!