How can I compare distance (not only between two points, but also a set of points)
12 views (last 30 days)
Show older comments
Anh Phan Viet
on 27 Feb 2020
Commented: Turlough Hughes
on 13 Mar 2020
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.
0 Comments
Accepted Answer
Turlough Hughes
on 27 Feb 2020
Edited: Turlough Hughes
on 2 Mar 2020
As I understand it, you have 10 by 100 points represented in variables X and Y which are both 10 by 100 matrices. A given point could then for example be defined as pt(i,j) = [X(i,j),Y(i,j)]
My understanding is that you want to calculate all distances between points in adjacent columns and store those which are less than the nominal threshold value R. So when comparing 10 points with 10 points of a given pair of adjacent columns this means looking at 100 distances overall.
For 2016b onwards you one solution would be to do the following:
R = 2000;
D = cell(99,1);
for j = 1:size(X,2)-1
distances = sqrt((X(:,j+1)-X(:,j).').^2 + (Y(:,j+1)-Y(:,j).').^2);
D{j,1} = distances(distances<R); % storing result in cell array D.
end
For ealier versions you would have to use bsxfun to carry out the same procedure, which would go as follows:
R = 2000;
D = cell(99,1);
for j = 1:size(X,2)-1
distances = sqrt(bsxfun(@minus,X(:,j+1),X(:,j).').^2 ...
+ bsxfun(@minus,Y(:,j+1),Y(:,j).').^2);
D{j,1} = distances(distances<R);
end
EDIT: Changed answer following comments below.
19 Comments
Turlough Hughes
on 13 Mar 2020
You can display the index numbers as I have shown above. If you want to reference the index to the original X and Y arrays you might do something like this for a given i:
x1 = X(ind{i}(:,1),i);
x2 = X(ind{i}(:,2),i+1);
y1 = Y(ind{i}(:,1),1);
y2 = Y(ind{i}(:,2),i+1);
More Answers (1)
Image Analyst
on 28 Feb 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 Comments
Image Analyst
on 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.
See Also
Categories
Find more on Matrices and Arrays 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!