How to do 2D/3D bivariate Interpolation of Non-gridded data?

7 views (last 30 days)
If I have following data set (given as <Data_Set.csv> and as 'Snapshot'). From which, I want to calculate interpolation of specific data point(s). What function is available? As following data is Non-gridded (non-uniform spaced) and not of same dimensions (means dimension of X-variable (4.5 to 8) <dimension-8> and dimension of Y-variable (1 to 300) <dimension-32>. Suppose I want the value from interpolation as query point of Xq=5.75 and Yq=25 or at any points inside the boundary of data set.

Accepted Answer

Image Analyst
Image Analyst on 23 Oct 2022
OK, maybe my demo was too complicated for you to figure out how to adapt it. But it's actually quite simple. Here it is:
data = readmatrix('Data_Set.csv')
data = 34×10
NaN NaN 4.5000 5.0000 5.5000 6.0000 6.5000 7.0000 7.5000 8.0000 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 1.0000 NaN 1.6474 2.0316 2.7526 3.9474 6.0421 9.5895 15.6053 25.5737 2.0000 NaN 2.6789 3.1105 3.8684 5.1316 7.2895 10.9579 17.2579 27.4526 3.0000 NaN 3.7105 4.1842 4.9474 6.2947 8.5000 12.2895 18.6947 29.0211 4.0000 NaN 4.7316 5.1947 6.0053 7.4000 9.6947 13.5526 20.0421 30.4895 5.0000 NaN 5.7316 6.2211 7.0947 8.4895 10.8474 14.7895 21.3158 31.8895 6.0000 NaN 6.7316 7.2316 8.0947 9.5789 11.9789 15.9895 22.5895 33.2789 7.0000 NaN 7.7316 8.2421 9.1368 10.6158 13.1053 17.1421 23.8263 34.6263 8.0000 NaN 8.7316 9.2632 10.1632 11.6842 14.2211 18.3421 25.0895 35.9789
yVector = data(3:end, 1);
xVector = data(1, 3:end);
values = data(3:end, 3:end);
[x, y] = meshgrid(xVector, yVector);
%================================ MAIN PART RIGHT HERE ==============================================
% Create the scattered interpolant. x, y, and gray levels must be column vectors so use (:) to make them column vectors.
% And grayLevels must be double or else an error gets thrown.
F = scatteredInterpolant(x(:), y(:), double(values(:)))
F =
scatteredInterpolant with properties: Points: [256×2 double] Values: [256×1 double] Method: 'linear' ExtrapolationMethod: 'linear'
% The above line creates an interpolant that fits a surface of the form v = F(x,y).
% Vectors x and y specify the (x,y) coordinates of the sample points.
% v is a vector that contains the sample values associated with the points (x,y).
% Find the value at a query location.
Xq=5.75
Xq = 5.7500
Yq=25
Yq = 25
z = F(Xq, Yq)
z = 28.1526
And if you look at your original data, the query point would be between index 3 and 4 in x and index 15 and 16 in y. The value of 28.1526 falls right in the middle of those 4 numbers in that square, so it seems like it's doing the interpolation correctly.
  2 Comments
NISHANT SURESHBHAI TEJANI
NISHANT SURESHBHAI TEJANI on 23 Oct 2022
Thank You. For the solution!
It also works with other Interpolation functions like 'interp2'.
z = interp2(x,y,double(values),5.75,25)
z = 28.1697
Image Analyst
Image Analyst on 23 Oct 2022
Yes but interp2 uses linear interpolation. Since your data maybe nonlinear I think scatteredInterpolant would be more accurate.
data = readmatrix('Data_Set.csv')
data = 34×10
NaN NaN 4.5000 5.0000 5.5000 6.0000 6.5000 7.0000 7.5000 8.0000 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 1.0000 NaN 1.6474 2.0316 2.7526 3.9474 6.0421 9.5895 15.6053 25.5737 2.0000 NaN 2.6789 3.1105 3.8684 5.1316 7.2895 10.9579 17.2579 27.4526 3.0000 NaN 3.7105 4.1842 4.9474 6.2947 8.5000 12.2895 18.6947 29.0211 4.0000 NaN 4.7316 5.1947 6.0053 7.4000 9.6947 13.5526 20.0421 30.4895 5.0000 NaN 5.7316 6.2211 7.0947 8.4895 10.8474 14.7895 21.3158 31.8895 6.0000 NaN 6.7316 7.2316 8.0947 9.5789 11.9789 15.9895 22.5895 33.2789 7.0000 NaN 7.7316 8.2421 9.1368 10.6158 13.1053 17.1421 23.8263 34.6263 8.0000 NaN 8.7316 9.2632 10.1632 11.6842 14.2211 18.3421 25.0895 35.9789
yVector = data(3:end, 1);
xVector = data(1, 3:end);
values = data(3:end, 3:end);
imshow(values, []);
[x, y] = meshgrid(xVector, yVector);
%================================ MAIN PART RIGHT HERE ==============================================
% Create the scattered interpolant. x, y, and gray levels must be column vectors so use (:) to make them column vectors.
% And grayLevels must be double or else an error gets thrown.
F = scatteredInterpolant(x(:), y(:), double(values(:)))
F =
scatteredInterpolant with properties: Points: [256×2 double] Values: [256×1 double] Method: 'linear' ExtrapolationMethod: 'linear'
% The above line creates an interpolant that fits a surface of the form v = F(x,y).
% Vectors x and y specify the (x,y) coordinates of the sample points.
% v is a vector that contains the sample values associated with the points (x,y).
% Find the value at a query location.
Xq=5.75
Xq = 5.7500
Yq=25
Yq = 25
z = F(Xq, Yq)
z = 28.1526

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 22 Oct 2022

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!