MATLAB Answers

Find distance and total number of overlapping points in a figure

조회 수: 4(최근 30일)
Sajjad Ahmad Khan
Sajjad Ahmad Khan 2021년 6월 24일
댓글: Sajjad Ahmad Khan 2021년 6월 26일
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
Sajjad Ahmad Khan
Sajjad Ahmad Khan 2021년 6월 26일
@Adam Danz thank you so much! I accepted the answer.

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

추가 답변(0개)

태그

Community Treasure Hunt

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

Start Hunting!

Translated by