Distance between points in a 2D matrix

6 次查看(过去 30 天)
M.T.
M.T. 2018-2-19
回答: Nihal 2024-7-16
Hello, I am trying to do a 2D interpolation by Kriging, but I have had difficulties with the step where I need to calculate the distance between the points in the observed values matrix. The matrix has 90 columns and 60 rows. I tried it with this code dist=zeros(size(RV)); for i=1:m %go through each column for j=1:n %go through each row dist(i,j)=sqrt(((RV(i,j+1)-RV(i+1,j+1))^2)+(RV(i,j+2)-RV(i+1,j+2))^2) But it doesn't work. I would appreciate it very much, if someone could help me solve this problem.

回答(1 个)

Nihal
Nihal 2024-7-16
To calculate the distances between points in a 2D grid for Kriging interpolation, you need to compute the pairwise distances between all points. Your current approach seems to have some issues, particularly with indexing that goes out of bounds. Let's correct and simplify the approach.
Below is an example of how you can calculate the distance matrix for a 2D grid using MATLAB. This example assumes your observed values matrix RV has dimensions 60 rows by 90 columns. We will create a distance matrix where each element ((i, j)) represents the Euclidean distance between the points ((i, j)) and ((k, l)).
Corrected Code
% Assuming RV is your observed values matrix with dimensions [60, 90]
[m, n] = size(RV);
% Preallocate distance matrix
dist = zeros(m*n, m*n);
% Create coordinate grid
[X, Y] = meshgrid(1:n, 1:m);
% Flatten the coordinate grids
X = X(:);
Y = Y(:);
% Calculate pairwise distances
for i = 1:m*n
for j = 1:m*n
dist(i, j) = sqrt((X(i) - X(j))^2 + (Y(i) - Y(j))^2);
end
end
% Display the distance matrix
disp('Distance Matrix:');
disp(dist);

类别

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