How to make the postions of each circle random?

4 views (last 30 days)
Hello Matlab Community,
I generated the random number of points (100 users) within 7 different circles (different radius) in square box of 1000 by 1000 meters as shown in figure. Here. the postions of clusters (circles) are fixed in each iterations as I shared in the plots for three iterations. how to make the positions of the cluster (circles) random in each iteration (for example for 10 iterations)?.
Thank You!
the code is here......
% Set the random number generator seed, for reproducibility
rng default
% Number of iterations to run the algorithm
NITER = 3;
% Number of seconds to pause after each figure
NPAUSE = 1;
% Fixed circle parameters
NCIRCLES = 7;
x0 = [100 450 500 650 900 900 800]';
y0 = [700 200 600 500 400 100 850]';
% Number of users
NUSERS = 100; % <--- Change this to 200
for ni = 1:NITER
userCircle = randi(NCIRCLES,NUSERS,1);
usersPerCircle = histcounts(userCircle,1:NCIRCLES+1)';
circleRadius = sqrt(usersPerCircle);
figure
axis square
set(gca,"Box","on")
grid on;
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
axis([0 1000 0 1000])
fontSize = 10;
xlabel('Position X (1000m)', 'FontSize', fontSize);
ylabel('Position Y (1000m)', 'FontSize', fontSize);
title('Random Users Within Circles', 'FontSize', fontSize);
axis([0 1000 0 1000])
hold on
for nc = 1:NCIRCLES
tnc = 2*pi*rand(usersPerCircle(nc),1);
rnc = 10*circleRadius(nc)*rand(usersPerCircle(nc),1);
x = x0(nc) + rnc.*cos(tnc);
y = y0(nc) + rnc.*sin(tnc);
h = plot(x,y,".");
set(h,'MarkerSize',8)
end
pause(NPAUSE)
end

Accepted Answer

Mathieu NOE
Mathieu NOE on 17 May 2022
hello
maybe this ? - as far as I understand you simply want to generate random center position for the circles ?
% Set the random number generator seed, for reproducibility
rng default
% Number of iterations to run the algorithm
NITER = 10;
% Number of seconds to pause after each figure
NPAUSE = 1;
% Fixed circle parameters
NCIRCLES = 7;
% x0 = [100 450 500 650 900 900 800]';
% y0 = [700 200 600 500 400 100 850]';
% Number of users
NUSERS = 100; % <--- Change this to 200
for ni = 1:NITER
userCircle = randi(NCIRCLES,NUSERS,1);
usersPerCircle = histcounts(userCircle,1:NCIRCLES+1)';
circleRadius = sqrt(usersPerCircle);
figure(ni)
axis square
set(gca,"Box","on")
grid on;
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
axis([0 1000 0 1000])
fontSize = 10;
xlabel('Position X (1000m)', 'FontSize', fontSize);
ylabel('Position Y (1000m)', 'FontSize', fontSize);
title('Random Users Within Circles', 'FontSize', fontSize);
axis([0 1000 0 1000])
hold on
% create random values for circles position
x0 = randi([100,800],NCIRCLES,1);
y0 = randi([100,800],NCIRCLES,1);
for nc = 1:NCIRCLES
tnc = 2*pi*rand(usersPerCircle(nc),1);
rnc = 10*circleRadius(nc)*rand(usersPerCircle(nc),1);
x = x0(nc) + rnc.*cos(tnc);
y = y0(nc) + rnc.*sin(tnc);
h = plot(x,y,".");
set(h,'MarkerSize',8)
end
pause(NPAUSE)
end
  8 Comments
Mathieu NOE
Mathieu NOE on 18 May 2022
ok no big problem here - don't spend too much time with that !

Sign in to comment.

More Answers (1)

Allen
Allen on 24 May 2022
Just change your x0 and y0 variables to random value vectors and move them to within your primary for loop.
% Set the random number generator seed, for reproducibility
rng default
% Number of iterations to run the algorithm
NITER = 3;
% Number of seconds to pause after each figure
NPAUSE = 1;
% Fixed circle parameters
NCIRCLES = 7;
% x0 = [100 450 500 650 900 900 800]';
% y0 = [700 200 600 500 400 100 850]';
% Number of users
NUSERS = 100; % <--- Change this to 200
for ni = 1:NITER
% Set x0 & y0 to random values. Using 900 as the maximum as this was
% the max value previously defined. Use whatever max is appropriate.
% Also, if x0 & y0 do not need to be integer values, use rand().
x0 = randi(900,[NCIRCLES,1]);
y0 = randi(900,[NCIRCLES,1]);
userCircle = randi(NCIRCLES,NUSERS,1);
usersPerCircle = histcounts(userCircle,1:NCIRCLES+1)';
circleRadius = sqrt(usersPerCircle);
figure
axis square
set(gca,"Box","on")
grid on;
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
axis([0 1000 0 1000])
fontSize = 10;
xlabel('Position X (1000m)', 'FontSize', fontSize);
ylabel('Position Y (1000m)', 'FontSize', fontSize);
title('Random Users Within Circles', 'FontSize', fontSize);
axis([0 1000 0 1000])
hold on
for nc = 1:NCIRCLES
tnc = 2*pi*rand(usersPerCircle(nc),1);
rnc = 10*circleRadius(nc)*rand(usersPerCircle(nc),1);
x = x0(nc) + rnc.*cos(tnc);
y = y0(nc) + rnc.*sin(tnc);
h = plot(x,y,".");
set(h,'MarkerSize',8)
end
pause(NPAUSE)
end

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by