# Find distance and total number of overlapping points in a figure

3 views (last 30 days)
I want to find the distance and total number of overlapping points as shown in the encircled ellipses in the attached figure. Also is there anyway I can set a distance formula so that the points lie within that distance would be counted? And how to find how many of those points are there which fall in that specific distance range? Thnaks
Adam Danz on 24 Jun 2021
I just noticed your tags 'image analysis'. The data in your demo are not image data. If the demo data does not represent the actual data, you'll need to clairify that. Otherwise, consider updating those tags.

Adam Danz on 24 Jun 2021
Edited: Adam Danz on 25 Jun 2021
The points are overlapping because of the marker size and the data resolution set by the axis limits. If you zoom in or use smaller marker size, those points will not overlap. You need to set a minimal distance threshold that defines what "overlapping" or "near" means.
You can use pdist or apply the distance formula to all pairs of points to get the distance between all pairs of points and then determine which paired-distances fall below your threshold.
Demo
This demo creates a set of (x,y) coordinates and uses pdist to compute the paired distances. It then plots a distribution of distances that you can use to choose a threshold that defines a distance considered to be 'overlapping'. Lastly, it marks the paired coordinates whose distances are below the threshold.
• xy is an nx2 matrix of [x,y] coordinates
• threshold is a value you choose to define 'near'
• nearIdx is a vector of indices showing which values in xy are 'near'
% Create (x,y) coordinates
rng('default')
xy = rand(30,2);
% Plot raw data
figure()
tiledlayout(2,2)
ax1 = nexttile();
plot(ax1, xy(:,1), xy(:,2), 'bo', 'MarkerSize', 10)
title(ax1, 'Raw data')
% Compute paired distances
pairedDist = squareform(pdist(xy)) + triu(inf(size(xy,[1,1])));
% Plot distribution of distances to choose a threshold
ax2 = nexttile();
histogram(ax2, pairedDist(:),50)
title(ax2, 'Paired distances')
threshold = 0.10;
xline(ax2, threshold, 'r-', 'Theshold')
% Find points that are below threshold distance
[xIdx,yIdx] = find(pairedDist < threshold);
nearIdx = [xIdx; yIdx];
% Plot the paired points that are 'near'
hold(ax1, 'on')
plot(ax1, xy(nearIdx,1), xy(nearIdx,2), 'rx')
% Set equal aspect ratio to visually inspect distances
axis(ax1, 'equal')