Clear Filters
Clear Filters

2D Interpolation & Extrapolation with non linear Sample grid points and nan

50 views (last 30 days)
Hi,
I have this 2D map wich I would like to interpolate to make square but also extrapolate.
interp2 does seem to work with nan inputs as it just returns same nan as output.
V=[1.029 1.000 1.060 1.048 nan nan nan
1.049 1.014 1.038 1.092 1.075 nan nan
nan 1.039 1.079 1.036 1.029 1.003 nan
nan nan 1.042 1.072 1.000 0.998 1.028
nan nan nan 1.038 1.024 1.076 1.022
nan nan nan nan 1.082 0.997 1.011];
X=[8 10 15 25 35 45 52];
Y=[5 10 16.25 22.5 28.75 35];
Xq=ones(numel(Y),numel(X)).*X;
Yq=ones(numel(Y),numel(X)).*Y';
Z=interp2(X,Y,V,Xq,Yq)
Z = 6×7
1.0290 1.0000 1.0600 1.0480 NaN NaN NaN 1.0490 1.0140 1.0380 1.0920 1.0750 NaN NaN NaN 1.0390 1.0790 1.0360 1.0290 1.0030 NaN NaN NaN 1.0420 1.0720 1.0000 0.9980 1.0280 NaN NaN NaN 1.0380 1.0240 1.0760 1.0220 NaN NaN NaN NaN 1.0820 0.9970 1.0110
I tried some inpaint functions from the community but none of them seems to be made to work with non linear sample grid points (0 2 7 17...).
I am open to suggestions
Thanks in Advance
Laurent
  4 Comments

Sign in to comment.

Accepted Answer

Voss
Voss on 24 Jun 2022
Edited: Voss on 24 Jun 2022
V = [ ...
1.029 1.000 1.060 1.048 nan nan nan
1.049 1.014 1.038 1.092 1.075 nan nan
nan 1.039 1.079 1.036 1.029 1.003 nan
nan nan 1.042 1.072 1.000 0.998 1.028
nan nan nan 1.038 1.024 1.076 1.022
nan nan nan nan 1.082 0.997 1.011]
V = 6×7
1.0290 1.0000 1.0600 1.0480 NaN NaN NaN 1.0490 1.0140 1.0380 1.0920 1.0750 NaN NaN NaN 1.0390 1.0790 1.0360 1.0290 1.0030 NaN NaN NaN 1.0420 1.0720 1.0000 0.9980 1.0280 NaN NaN NaN 1.0380 1.0240 1.0760 1.0220 NaN NaN NaN NaN 1.0820 0.9970 1.0110
X = [8 10 15 25 35 45 52]; % using the X and Y from your code rather than
Y = [5 10 16.25 22.5 28.75 35]; % from your image, which are 5 less
% make a scatteredInterpolant of the points where V is non-NaN
[XX,YY] = meshgrid(X,Y);
idx = ~isnan(V(:));
I = scatteredInterpolant(XX(idx),YY(idx),V(idx));
% interpolate/extrapolate to all points
Z = reshape(I(XX,YY),numel(Y),[])
Z = 6×7
1.0290 1.0000 1.0600 1.0480 1.0831 1.0621 0.9944 1.0490 1.0140 1.0380 1.0920 1.0750 1.0286 1.0002 1.0322 1.0390 1.0790 1.0360 1.0290 1.0030 1.0234 1.0147 1.0212 1.0420 1.0720 1.0000 0.9980 1.0280 0.9854 0.9911 1.0106 1.0380 1.0240 1.0760 1.0220 0.9513 0.9626 0.9872 1.0543 1.0820 0.9970 1.0110
% visualization
x_lim = [min(X) max(X)];
y_lim = [min(Y) max(Y)];
z_lim = [min(Z(:)) max(Z(:))];
temp = {V Z};
names = {'V' 'Z'};
for ii = [1 2]
subplot(2,1,ii)
surface(XX,YY,temp{ii},'FaceColor','interp')
set(gca(),'YDir','reverse')
view(2)
xlim(x_lim);
ylim(y_lim);
xlabel('X');
ylabel('Y');
colorbar();
caxis(z_lim);
title(names{ii});
end

More Answers (0)

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!