Distance between points in a 2D matrix

13 views (last 30 days)
M.T.
M.T. on 19 Feb 2018
Edited: VBBV on 11 Feb 2025
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.

Answers (2)

Hornett
Hornett on 16 Jul 2024
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);

VBBV
VBBV on 11 Feb 2025
Edited: VBBV on 11 Feb 2025
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+1,j)-RV(i+1,j+1))^2)
end
end

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!