# cluster points within given radius

5 views (last 30 days)
B!nd on 27 Nov 2021
Answered: Image Analyst on 27 Nov 2021
I have generated two sets of random variables a and b. I want to know the number of 'a' points in the vicinity of each b with given radius. Is there a way to do this.
n = 300;
R = 350;
x0 = 50; % Center of the circle in the x direction.
y0 = 90; % Center of the circle in the y direction.
angle1 = 0;
angle2 = 2*pi;
t = (angle2 - angle1) * rand(n,1) + angle1;
r = R*sqrt(rand(n,1));
x = x0 + r.*cos(t);
y = y0 + r.*sin(t);% first set of values
n2 = 25;
R = 350;
t = (angle2 - angle1) * rand(n2,1) + angle1;
r = R*sqrt(rand(n2,1));
x1 = x0 + r.*cos(t);
y1 = y0 + r.*sin(t);% second set of values
% Now display our random set of points in a figure.
plot(x,y, '.', 'MarkerSize', 5,'Color','blue')
hold on
plot(x1,y1, '.', 'MarkerSize', 5,'Color','red')
hold off

Chunru on 27 Nov 2021
n = 300;
R = 350;
x0 = 50; % Center of the circle in the x direction.
y0 = 90; % Center of the circle in the y direction.
angle1 = 0;
angle2 = 2*pi;
t = (angle2 - angle1) * rand(n,1) + angle1;
r = R*sqrt(rand(n,1));
x = x0 + r.*cos(t);
y = y0 + r.*sin(t);% first set of values
n2 = 25;
R = 350;
t = (angle2 - angle1) * rand(n2,1) + angle1;
r = R*sqrt(rand(n2,1));
x1 = x0 + r.*cos(t);
y1 = y0 + r.*sin(t);% second set of values
% Now display our random set of points in a figure.
plot(x,y, '.', 'MarkerSize', 5,'Color','blue')
hold on
plot(x1,y1, '.', 'MarkerSize', 5,'Color','red')
hold off
npoints = zeros(size(x1));
for j=1:n2
npoints(j) = sum( (x-x1(j)).^2 + (y-y1(j)).^2 <= radius.^2 );
end
npoints'
ans = 1×25
7 8 2 7 2 1 4 5 5 4 11 8 6 7 5 6 4 5 6 6 11 7 7 1 7

Image Analyst on 27 Nov 2021
Use pdist2(), then thresholding, and finally summing:
%======================================================================================
clc; % Clear the command window.
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 = 15;
n = 300;
R = 350;
x0 = 50; % Center of the circle in the x direction.
y0 = 90; % Center of the circle in the y direction.
angle1 = 0;
angle2 = 2*pi;
t = (angle2 - angle1) * rand(n,1) + angle1;
r = R*sqrt(rand(n,1));
x = x0 + r.*cos(t);
y = y0 + r.*sin(t);% first set of values
n2 = 25;
R = 350;
t = (angle2 - angle1) * rand(n2,1) + angle1;
r = R*sqrt(rand(n2,1));
x1 = x0 + r.*cos(t);
y1 = y0 + r.*sin(t);% second set of values
% Now display our random set of points in a figure.
plot(x,y, '.', 'MarkerSize', 5,'Color','blue')
hold on
plot(x1,y1, '.', 'MarkerSize', 5,'Color','red')
hold off
%======================================================================================
% My code
distancesMatrix = pdist2([x,y], [x1,y1]);
% Get distances of set x1,y1 to set x,y.
neighborDistance = 50;
withinDistance = distancesMatrix <= neighborDistance;
% Get how many points of (x1,y1) are to each point in set (x,y)
numSet1CloseToSet = sum(withinDistance, 2);
% Get how many points of (x,y) are to each point in set (x1,y1)
numSetCloseToSet1 = sum(withinDistance, 1);