# How can I compare distance (not only between two points, but also a set of points)

10 views (last 30 days)
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.

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
To understand why I transpose column j, see the following documentation.
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

Turlough Hughes on 13 Mar 2020
As follows:
R = 2000;
D = cell(99,1);
ind = cell(99,1);
for i = 1:size(X,2)-1
distances = sqrt(bsxfun(@minus,X(:,i+1),X(:,i).').^2 ...
+ bsxfun(@minus,Y(:,i+1),Y(:,i).').^2);
[iNext,iCurr] = find(distances<R); % iNext: index for column i+1, iCurr: index for column i
D{i,1} = distances(iNext,iCurr);
ind{i,1} = [iCurr,iNext];
end
Results
>> ind{1}
ans =
9 7
>> ind{2}
ans =
6 8
7 7
Anh Phan Viet on 13 Mar 2020
Thank you, and then if I code find(ind{1,1},'row'), will it display 9. I've tried but it's wrong. So can you tell me how to display 9, and 7
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);

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.

Anh Phan Viet on 1 Mar 2020
I am so sorry for not corresponding. For me, X is matrix (10*100), Y is matrix (10*100) represent the coordinates (X,Y). In reality, this is the tracking model. I've devided 1000 points into 100 parts and compared. Each part has 10 points. That's why I have matrix 10*100. In the field of radar system, it means that I have 100 steps. At step 1 when I turn on the radar scanning system, I realize 10 points. At step 2 (t2 = t1 + deltat) when I turn on the radar scanning system, I realize other 10 points, up to step 100. I've known that at each step when I saw 10 points, only 1 point is real, 9 other points are false alarms. Therefore I need to compare the distance between set of point at collum 1 and at collum 2, next will be collum 2 and collum 3, 3 and 4, 4 and 5. Finally 99 and 100. If the distance is smaller than R (a value that I called), I chose these points
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.
Anh Phan Viet on 2 Mar 2020
Thank you very much

R2016a

### Community Treasure Hunt

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

Start Hunting!