How can I compare distance (not only between two points, but also a set of points)
Afficher commentaires plus anciens
I have a set of points (coordinates X, Y). X is matrix 10*100, also Y.
I 've devided these points into 100 collums, 1 collum has 10 points with coordinate (X,Y) and compared distance between one point at one collum and another point at collum, which is next to the previous collum.
For example, I compared these points from collum 1 and collum 2, and chose the distances, which are smaller than distance R
for i = 1:10
X_collum1(i,1) = X(i,1);
X_collum2(i,1) = X(i,2);
end
for i = 1:10
Y_collum1(i,1) = Y(i,1);
Y_collum2(i,1) = Y(i,2);
end
for i = 1:10,
for j = 1:10
X_distance(i,j) = abs(X_collum1(i,1) - X_collum2(j,1));
Y_distance(i,j) = abs(Y_collum1(i,1) - Y_collum2(j,1));
a(i,j) = sqrt(X_distance(i,j)^2+Y_distance(i,j)^2);
if a(i,j) < R
distance = a(i,j);
end
end
end
Next step is comparing between collum 2 and 3, then 3 and 4, then 4 and 5. Finally this will be between collum 99 and 100. But I can't do this 99 times. Therefore, I need someone, who comes up with some ideas to do this task quickly (like making some loop, or so on). Thanks a lot.
Réponse acceptée
Plus de réponses (1)
Image Analyst
le 28 Fév 2020
If you have the Statistics and Machine Learning toolbox, use pdist2(). It will give you the distance of every point to every other point. Split your 10x100 matrices up into some number of N-by-2 lists of (x,y) coordinates and pass them in, like
distances = pdist2(xy, xy);
Let me know if you still can't figure it out.
5 commentaires
Anh Phan Viet
le 29 Fév 2020
Image Analyst
le 29 Fév 2020
See this:
X = rand(4, 1)
Y = rand(4, 1)
xy = [X, Y]
You'll see something like:
X =
0.42176
0.91574
0.79221
0.95949
Y =
0.65574
0.035712
0.84913
0.93399
xy =
0.42176 0.65574
0.91574 0.035712
0.79221 0.84913
0.95949 0.93399
For your case where X is 10 rows-by-100 columns, and Y is also 10 rows-by-100 columns, please explain what each row and each column represents. Which row(s) or column(s) represent the x and y values?
Anh Phan Viet
le 1 Mar 2020
Image Analyst
le 1 Mar 2020
This will compute the distances between every point in each column with every point in every column.
X = rand(10, 100);
Y = rand(10, 100);
[rows, columns] = size(X)
distances = zeros(rows, rows, columns);
plane = 1;
% For each pair of columns, get a 10-by-10 matrix that gives
% the distances between one columns and another
for col1 = 1 : columns
xySet1 = [X(:, col1), Y(:, col1)];
for col2 = 1 : columns
xySet2 = [X(:, col2), Y(:, col2)];
distances(:, :, plane) = pdist2(xySet1, xySet2);
plane = plane + 1;
end
end
% Show all 10x10x100 distances in command window
distances
For simplicity in indexing the answers, I did not exclude comparing each column with itself (e.g. there will be comparisons of column 1 with column1, column 2 with column 2, ... column 100 with column 100) but all the distances are in there and if I were to exclude same columns, the indexing would be much more complicated.
To find distances that are less than R, you can create a binary "map" where it's 1 if the distance is less than R
mapOfClosePoints = distances > 0 & distances < R;
Note that by also comparing to non-zero, we will not be selecting columns where the two columns are the same column.
Anh Phan Viet
le 2 Mar 2020
Catégories
En savoir plus sur Polar Plots dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!