Find distance and total number of overlapping points in a figure

조회 수: 3 (최근 30일)
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
Adam Danz
Adam Danz 2021년 6월 24일
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
Adam Danz 2021년 6월 24일
편집: Adam Danz 2021년 6월 25일
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
Adam Danz
Adam Danz 2021년 6월 25일
편집: Adam Danz 2021년 6월 25일
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)
ans = 2
Note that there are two points there. Let's see if they are exact duplicates,
xy(idx,:)
ans = 2×2
201.6977 155.1422 201.6977 155.1422
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)
ans = 57
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.
Sajjad Ahmad Khan
Sajjad Ahmad Khan 2021년 6월 26일
@Adam Danz thank you so much! I accepted the answer.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by