Find distance of two points inside a matrix

조회 수: 8 (최근 30일)
lena kappa
lena kappa 2023년 6월 7일
댓글: lena kappa 2023년 6월 8일
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?

채택된 답변

Kautuk Raj
Kautuk Raj 2023년 6월 8일
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:

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Interpolation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by