Interpolate y and y using a 3D vector with similar length

Hello, I have a a matrix with 3 coordinates (each column) x, y, and z. Each column has the same length. I would like to interpolate a especific coordinate xi and yi to obtain zi using the main matriz. Besides, I would like to generate a surface plot using the codinates x,y and z. I know that z should be a matrix mxn in order to do that based on the number of elements m and n according to x and y, respectively. It is worth mentioning that x, y and z are longitude, latitude and depth respectively. I would appreciate the help.

 Accepted Answer

To do the relatively simple first interpolation, use the interp2 function.
Tto draw the surface plot, the best option is the scatteredInterpolant function.
Both of these are relatively easy to use. I can help with specific problems using them.

7 Comments

Hi, Thank you for your answer. I was using interp2 but despite I transposed x,y,and z just in case, I got the following message:
Error using griddedInterpolant
Interpolation requires at least two sample points for each grid dimension.
Error in interp2>makegriddedinterp (line 226)
F = griddedInterpolant(varargin{:});
Error in interp2 (line 126)
F = makegriddedinterp({X, Y}, V, method,extrap);
That is why I asked another solution. Thank you
I attached the txt file. The first, second and third column are x,y and z. The extrapolated points could be xi=-71.7010; yi=-34.7780 in order to find zi.
As always, my pleasure!
I need a bit of clarification. Which columns correspond to ‘x’ and ‘y’ and which is ‘z’? (It is probably best to use scatteredInterpolant for this entire project.) To begin with, the data appear to be gridded, so using reshape would work for the surface plot —
T1 = readtable('data_example.txt')
T1 = 64800x4 table
Longitude Latitude MohoDepth sigma _________ ________ _________ _____ -179.5 89.5 13.52 6.41 -178.5 89.5 13.53 6.4 -177.5 89.5 13.55 6.4 -176.5 89.5 13.57 6.39 -175.5 89.5 13.58 6.38 -174.5 89.5 13.6 6.38 -173.5 89.5 13.61 6.37 -172.5 89.5 13.63 6.37 -171.5 89.5 13.64 6.36 -170.5 89.5 13.66 6.35 -169.5 89.5 13.67 6.35 -168.5 89.5 13.69 6.34 -167.5 89.5 13.7 6.34 -166.5 89.5 13.72 6.33 -165.5 89.5 13.73 6.32 -164.5 89.5 13.75 6.32
VN = T1.Properties.VariableNames;
[Uy,ix] = unique(T1.Latitude, 'stable');
ixd = diff(ix);
ne360 = find(ixd ~= 360)
ne360 = 0x1 empty double column vector
N = mean(ixd)
N = 360
Lngr = reshape(T1.Longitude, N, []);
Latr = reshape(T1.Latitude, N, []);
Mohr = reshape(T1.MohoDepth, N, []);
sigr = reshape(T1.sigma, N, []);
figure
surf(Lngr, Latr, Mohr, 'EdgeColor','interp')
colormap(turbo)
colorbar
xlabel(VN{1})
ylabel(VN{2})
zlabel(VN{3})
figure
surf(Lngr, Latr, sigr, 'EdgeColor','interp')
colormap(turbo)
colorbar
xlabel(VN{1})
ylabel(VN{2})
zlabel(VN{4})
What do you want to interpolate?
EDIT — (16 Jul 21024 at 00:45)
By the way, xi and yi are longitude and latitude and zi is the moho depth.
Thank you. I used scatteredInterpolant for that, and stem3 to plot it —
MohoFcn = scatteredInterpolant(T1.Longitude, T1.Latitude, T1.MohoDepth)
MohoFcn =
scatteredInterpolant with properties: Points: [64800x2 double] Values: [64800x1 double] Method: 'linear' ExtrapolationMethod: 'linear'
xi=-71.7010;
yi=-34.7780;
zi = MohoFcn(xi, yi)
zi = 35.4194
figure
surf(Lngr, Latr, Mohr, 'EdgeColor','interp', 'FaceAlpha',0.25, 'EdgeAlpha',0.25)
hold on
stem3(xi, yi, zi, 'vr', 'MarkerFaceColor','r')
hold off
colormap(turbo)
colorbar
xlabel(VN{1})
ylabel(VN{2})
zlabel(VN{3})
.
Thank you very much. I would like to extrapolate as an example xi=-71.7010; yi=-34.7780 in order to find zi. Or I can also create a vector xi and yi with a specific length of extrapolated values to obtain the vector zi with similar length. By the way, xi and yi are longitude and latitude and zi is the moho depth.
Thank you!
in the EDIT at the end, I added a scateredInterpolant call to calculate and display the value of the interpolated point, and a separate surf call (with more transparent surface and edges than the earlier one) to my answer, showing the interpolated point in the plot.
I appreciate your help. Thank you very much :).

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2023a

Community Treasure Hunt

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

Start Hunting!