How to make the postions of each circle random?

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

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, Yes exactly perfect, but some of the circles after few iterations are overlapping somehow. How to avoid the overlapping in the circles or how to have a proper gaps between the circles at each iterations?
Thank you so much for the response!!!
hello again
so the next trick is that I added a while loop to get enough spacing - here the threshold is set to gap = 3 times the max radius of the data
clc
clearvars
% 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
k = 0;
userCircle = randi(NCIRCLES,NUSERS,1);
usersPerCircle = histcounts(userCircle,1:NCIRCLES+1)';
% circleRadius = sqrt(usersPerCircle);
circleRadius = 10*sqrt(usersPerCircle);
circleRadius_max = 10*ceil(max(circleRadius*0.1)); % rounded to upper ten's
min_gap = 3*circleRadius_max; % min gap between groups - adapt factor here (min = 2)
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
a = randi([circleRadius_max,1000-circleRadius_max],NCIRCLES,2);
x0 = a(:,1);
y0 = a(:,2);
d0 = sqrt((x0-x0').^2 + (y0-y0').^2);
min_d0 = min(d0(d0>0));
while min_d0 <= min_gap % retry with new data if condition of gap distance is not met
a = randi([circleRadius_max,1000-circleRadius_max],NCIRCLES,2);
x0 = a(:,1);
y0 = a(:,2);
d0 = sqrt((x0-x0').^2 + (y0-y0').^2);
min_d0 = min(d0(d0>0));
k = k+1;
end
k % shows iterations amount to get a new valid set of coordinates that fullfil the required gap distance
for nc = 1:NCIRCLES
tnc = 2*pi*rand(usersPerCircle(nc),1);
% rnc = 10*circleRadius(nc)*rand(usersPerCircle(nc),1);
rnc = 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)
hold off
end
@Mathieu NOE Perfectly Okay, Thank you so much for your precous time and response...
as always, my pleasure !
would you mind accepting my answer ? tx
Yes, I would love to accept your answere
Make sure you are logged in. If the question has already been closed you will see "Accepted Answer by ____" to the right of your profile picture where the question is asked. If not, then look next to the profile picture of the answerer(s). To the right of each person's answer, in green, you should see a box that says "Accept this answer." Push that button.
I am already logged in but there is no such options I re-logged in but still no option even I am refreshing the webpage
ok no big problem here - don't spend too much time with that !

Sign in to comment.

More Answers (1)

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

Categories

Find more on Simulation in Help Center and File Exchange

Products

Release

R2021b

Asked:

on 17 May 2022

Answered:

on 24 May 2022

Community Treasure Hunt

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

Start Hunting!