points too close XY space
16 次查看(过去 30 天)
显示 更早的评论
Hello,
I have a set of N points with coordinates (x1,y1)...(xN, yN). I would like to find the pairs that are closer than DX in X and closer than DY in Y, avoiding for-loops. Thanks in advance for any help!
1 个评论
James Tursa
2017-6-15
How large is N? How is the data currently stored? What do you want for output? (e.g., a logical NxN matrix, or ...?)
采纳的回答
Image Analyst
2017-6-17
Here is a solution that shows you all pairs of points where delta x is less than 0.2:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
% Define number of points.
N = 50;
% Get random x,y coordinates for those points.
x = rand(N, 1);
y = rand(N, 1);
subplot(2, 2, 1);
plot(x, y, 'b.', 'MarkerSize', 18);
grid on;
% Get distance of every point to every other point.
distances = pdist2(x, x);
% Find points closer than some specified distance to each other.
closestAllowableDistance = 0.02;
closePairs = distances < closestAllowableDistance;
subplot(2, 2, 2);
imshow(closePairs, []);
axis on;
xlabel('Column Index');
ylabel('Row Index');
% Show circles around pairs that are close
subplot(2, 2, 3);
plot(x, y, 'b.', 'MarkerSize', 18);
grid on;
hold on;
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
drawnow;
% Show circles around pairs that are close
hPair = plot(0, 0); % Initialize so we have something to delete the first time though the loop.
for col = 1 : N
for row = 1 : col-1
% Plot this point
if closePairs(row, col)
% The pair is close, so plot it.
x1 = x(row);
y1 = y(row);
x2 = x(col);
y2 = y(col);
% Delete old point
delete(hPair);
% Plot next point.
hPair = plot([x1, x2], [y1, y2], 'ro', 'MarkerSize', 13, 'LineWidth', 2);
% Tell them how far apart they are, and ask them if they want to continue.
promptMessage = sprintf('This pair is separated by %f\n\nDo you want to Continue processing,\nor Quit processing?',...
distances(row, col));
titleBarCaption = 'Continue?';
buttonText = questdlg(promptMessage, titleBarCaption, 'Continue', 'Quit', 'Continue');
if strcmpi(buttonText, 'Quit')
return;
end
end
end
end
2 个评论
Image Analyst
2017-6-19
You're welcome. Thanks for accepting. It's not a full solution. It's just for delta X. You have to basically repeat most of it for delta Y.
更多回答(3 个)
Julian Hapke
2017-6-15
assuming only neighboring points are of interest:
% find close points, add first index because diff is one shorter than x
idxx = [false;diff(x(:))<dx];
% for close points also mark the previous one, so you get both an not only the latter
idxx = idxx | circshift(idxx,[-1,0]);
same goes for y
0 个评论
Image Analyst
2017-6-16
编辑:Image Analyst
2017-6-16
Use pdist2() from the Statistics and Machine Learning Toolbox to get all distances from every point to every other point without loops, all in one function call. Try this demo. (My loops are only to display the close pairs with red circles around them, not to compute the separation distances).
clearvars;
clc;
close all;
% Define number of points.
N = 50;
% Get random x,y coordinates for those points.
xy = rand(N, 2);
subplot(2, 2, 1);
plot(xy(:, 1), xy(:, 2), 'b.', 'MarkerSize', 18);
grid on;
% Get distance of every point to every other point.
distances = pdist2(xy, xy);
% Find points closer than some specified distance to each other.
closestAllowableDistance = 0.05;
closePairs = distances < closestAllowableDistance;
subplot(2, 2, 2);
imshow(closePairs, []);
axis on;
xlabel('Column Index');
ylabel('Row Index');
% Show circles around pairs that are close
subplot(2, 2, 3);
plot(xy(:, 1), xy(:, 2), 'b.', 'MarkerSize', 18);
grid on;
hold on;
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
drawnow;
% Show circles around pairs that are close
hPair = plot(0, 0); % Initialize so we have something to delete the first time though the loop.
for col = 1 : N
for row = 1 : col-1
% Plot this point
if closePairs(row, col)
% The pair is close, so plot it.
x1 = xy(row, 1);
y1 = xy(row, 2);
x2 = xy(col, 1);
y2 = xy(col, 2);
% Delete old point
delete(hPair);
% Plot next point.
hPair = plot([x1, x2], [y1, y2], 'ro', 'MarkerSize', 13, 'LineWidth', 2);
% Tell them how far apart they are, and ask them if they want to continue.
promptMessage = sprintf('This pair is separated by %f\n\nDo you want to Continue processing,\nor Quit processing?',...
distances(row, col));
titleBarCaption = 'Continue?';
buttonText = questdlg(promptMessage, titleBarCaption, 'Continue', 'Quit', 'Continue');
if strcmpi(buttonText, 'Quit')
return;
end
end
end
end
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/176971/image.png)
1 个评论
Julian Hapke
2017-6-16
编辑:Julian Hapke
2017-6-16
I understood that DF asked for close DX and close DY, the distance for points close in x-direction but far apart in y direction is still big, so no match. also: inside if pdist2 there are several for loops. One could use the good ol' bsxfun to calculate the distances between all the points:
distx = bsxfun(@minus,x,x')
disty = bsxfun(@minus,y,y')
dist = x.^2+y.^2; % no need for sqrt if you check against the squared threshold
Edit: really nice visualization of your answer!
DF
2017-6-17
1 个评论
Image Analyst
2017-6-17
编辑:Image Analyst
2017-6-17
Simply call pdist2() on the X and Y separately, then threshold the distances matrix at DX and DY. It's easy. If you don't think so, then give me the range of x and y and the values for DX and DY and the number of points. I'm not sure why you're not sharing any diagram with me to help me visualize your situation. Why leave me guessing???
See my second answer.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Object Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!