Find distance and total number of overlapping points in a figure
7 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Sajjad Ahmad Khan
am 24 Jun. 2021
Kommentiert: Sajjad Ahmad Khan
am 26 Jun. 2021
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
1 Kommentar
Adam Danz
am 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.
Akzeptierte Antwort
Adam Danz
am 24 Jun. 2021
Bearbeitet: Adam Danz
am 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')
6 Kommentare
Adam Danz
am 25 Jun. 2021
Bearbeitet: Adam Danz
am 25 Jun. 2021
You have to explore your data a bit more carefully.
> but there are rectangular regions which only have one circle
To investigate this, I loaded the attached data and searched for data points within the highlighted rectange which has the following coordinates.
data = load('XYlocalizations.mat');
xy = data.XY;
idx = xy(:,1)<202 & xy(:,1)>200 & xy(:,2)<155.5 & xy(:,2)>155;
sum(idx)
Note that there are two points there. Let's see if they are exact duplicates,
xy(idx,:)
Yep, you've got two coordinates in exactly the same location.
Now let's look at how many 2D points are exact matches,
pairedDist = squareform(pdist(xy)) + triu(inf(size(xy,[1,1])));
sum(pairedDist(:)==0)
You've got 57 points that have duplicate (x,y) coordinates.
Finally, let's label coordinates that contain duplicates using a black square
% Continuing from the code within my answer...
axis(ax1, 'equal')
xlim(ax1, [174.09, 212.12])
ylim(ax1, [135.43, 165.75])
threshold0 = 0; % for near-duplicates, use something like 0.001
[x0Idx,y0Idx] = find(pairedDist <= threshold0);
duplicateIdx = [x0Idx; y0Idx];
plot(ax1, xy(duplicateIdx,1), xy(duplicateIdx,2), 'ks','Markersize',15, 'LineWidth', 1)
Since my answer solved your question, please consider accepting the answer (blue accept-this-answer button) so the question is marked as solved.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Line Plots finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!