points too close XY space

10 views (last 30 days)
DF
DF on 15 Jun 2017
Commented: Image Analyst on 19 Jun 2017
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 Comment
James Tursa
James Tursa on 15 Jun 2017
How large is N? How is the data currently stored? What do you want for output? (e.g., a logical NxN matrix, or ...?)

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 17 Jun 2017
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 Comments
DF
DF on 19 Jun 2017
Thank you very much! This answers my question. I was not expecting a full working script, but just some indications. Thanks again!
Image Analyst
Image Analyst on 19 Jun 2017
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.

Sign in to comment.

More Answers (3)

Julian Hapke
Julian Hapke on 15 Jun 2017
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

Image Analyst
Image Analyst on 16 Jun 2017
Edited: Image Analyst on 16 Jun 2017
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
  1 Comment
Julian Hapke
Julian Hapke on 16 Jun 2017
Edited: Julian Hapke on 16 Jun 2017
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!

Sign in to comment.


DF
DF on 17 Jun 2017
Thank you all for your answers! However, I think my request was not very clear: I'm not interested in the (Euclidean) distance between the points, but I need to know which pairs of points are closer than DX in X AND DY in Y. The output can be either a NxN logical matrix or the first pair found that is too close in DX and DY.
  1 Comment
Image Analyst
Image Analyst on 17 Jun 2017
Edited: Image Analyst on 17 Jun 2017
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.

Sign in to comment.

Categories

Find more on Introduction to Installation and Licensing 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!