cluster points within given radius

6 次查看(过去 30 天)
B!nd
B!nd 2021-11-27
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

回答(2 个)

Chunru
Chunru 2021-11-27
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
radius = 50;
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
Image Analyst 2021-11-27
Use pdist2(), then thresholding, and finally summing:
%======================================================================================
% Your code
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);

Community Treasure Hunt

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

Start Hunting!

Translated by