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!
distancepdist2Statistics and Machine Learning Toolbox
format longg;format compact;clc; % Clear command window.
workspace; % Make sure the workspace panel is showing.
fontSize = 25;% Read in a standard MATLAB gray scale demo image.
folder = 'C:\Users\Naresh\Documents\Temporary';baseFileName = 'imfill.jpg';% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);% Check if file exists.
if ~exist(fullFileName, 'file') % File doesn't exist -- didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file') % Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName); uiwait(warndlg(errorMessage)); return; endendgrayImage = imread(fullFileName);% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(grayImage)% Display the original gray scale image.
subplot(2, 2, 1);imshow(grayImage, []);axis square;title('Original Grayscale Image', 'FontSize', fontSize);% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);% Give a name to the title bar.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off') % Create a binary image.
binaryImage = grayImage < 128;subplot(2, 2, 2);imshow(binaryImage);axis square;title('Binary Image', 'FontSize', fontSize);% bwboundaries() returns a cell array, where each cell contains the row/column coordinates for an object in the image.
% Plot the borders of all the coins on the original grayscale image using the coordinates returned by bwboundaries.
% Display the original gray scale image.subplot(2, 2, 3);imshow(grayImage, []);title('Original Grayscale Image with Outlines from bwboundaries()', 'FontSize', fontSize);axis square;hold on;boundaries = bwboundaries(binaryImage);numberOfBoundaries = size(boundaries, 1);for k = 1 : numberOfBoundaries thisBoundary = boundaries{k}; plot(thisBoundary(:,2), thisBoundary(:,1), 'r-', 'LineWidth', 3);endhold off;
clc; % Clear the command window.
fprintf('Beginning to run %s.m.\n', mfilename);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 = 22;numPoints = 200;pointsPerCluster = 5;numClusters = ceil(numPoints / pointsPerCluster)coordinates = zeros(pointsPerCluster, 2, numClusters);t = table(zeros(numPoints, 1), zeros(numPoints, 1), zeros(numPoints, 1), zeros(numPoints, 1), 'VariableNames', {'ClusterNumber', 'PointNumber', 'x', 'y'});xy = rand(numPoints, 2);x = xy(:, 1);y = xy(:, 2);subplot(1, 2, 1);plot(x, y, 'b.', 'MarkerSize', 14);grid on;% Get distances of every point to every other point
distances = pdist2(xy, xy);minDistance = min(distances(distances~=0))[row, col] = find(distances == minDistance)currentRow = row(1) % Get first point.
pointer = 1;for k = 1 : numClusters % Get distances of every point to every other point distances = pdist2(xy, xy(currentRow, :)); % Find the closest points.
minDistances = mink(distances, pointsPerCluster); [ia, ib] = ismember(distances, minDistances); rows = find(ib); % Store these coordinates as cluster #k
for n = 1 : length(rows) t.ClusterNumber(pointer) = k; t.PointNumber(pointer) = n; t.x(pointer) = xy(rows(n), 1); % Store x value.
t.y(pointer) = xy(rows(n), 2); % Store y value.
pointer = pointer + 1; end if pointer >= numPoints break; % Quit when all points are used up
end % Set the current row coordinates to infinity so we know not to consider (use) them again.
xy(rows, :) = inf; % Get new cluster -- new starting point.
% Get distances of every point to every other point distances = pdist2(xy, xy); minDistance = min(distances(distances~=0)); [row, col] = find(distances == minDistance); currentRow = row(1); % Get first point.end% Show clusters in unique colors
subplot(1, 2, 2);gscatter(t.x, t.y, t.ClusterNumber);
Best Answer