cluster points within given radius
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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
0 Kommentare
Antworten (2)
Chunru
am 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
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'
0 Kommentare
Image Analyst
am 27 Nov. 2021
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);
0 Kommentare
Siehe auch
Kategorien
Mehr zu Visualization 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!