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

12 次查看(过去 30 天)
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
Turlough Hughes 2020-2-27
编辑:Turlough Hughes 2020-3-2
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
EDIT: Changed answer following comments below.
  19 个评论
Anh Phan Viet
Anh Phan Viet 2020-3-13
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
Turlough Hughes 2020-3-13
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);

请先登录,再进行评论。

更多回答(1 个)

Image Analyst
Image Analyst 2020-2-28
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 个评论
Image Analyst
Image Analyst 2020-3-1
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

产品


版本

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by