Find distance of two points inside a matrix

3 views (last 30 days)
Hello everyone! So I have a meshgrid of x and y and an other matrix that gives me the z corresponding to the same indexes.I want to choose an x1,y1 value and an x2,y2 value and interpolate all the z in between those to coordinates?How can I do this?

Accepted Answer

Kautuk Raj
Kautuk Raj on 8 Jun 2023
The interp2 function in MATLAB to interpolate the depths between two points on a grid of longitudes and latitudes. I will show an example here:
% Create a meshgrid of longitudes and latitudes
lon = linspace(-180, 180, 361);
lat = linspace(-90, 90, 181);
[lon_grid, lat_grid] = meshgrid(lon, lat);
% Create a matrix of depths corresponding to the grid
depths = rand(size(lon_grid));
% Choose two points to interpolate between
x1 = -100;
y1 = 30;
x2 = 50;
y2 = 60;
% Interpolate the depths between the two points
interp_lon = linspace(x1, x2, 100);
interp_lat = linspace(y1, y2, 100);
interp_depths = interp2(lon_grid, lat_grid, depths, interp_lon, interp_lat);
% Plot the original depths and the interpolated depths
figure
subplot(2, 1, 1)
pcolor(lon_grid, lat_grid, depths)
shading interp
hold on
plot(x1, y1, 'r.', x2, y2, 'r.')
colorbar
title('Original Depths')
xlabel('Longitude')
ylabel('Latitude')
subplot(2, 1, 2)
plot(interp_lon, interp_depths, 'r')
hold on
plot(lon_grid, depths, 'b')
legend('Interpolated Depths', 'Original Depths')
title('Interpolated Depths')
xlabel('Longitude')
ylabel('Depth')
The output looks like:

More Answers (0)

Categories

Find more on Interpolation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!