How to distribute random number of users within the different circles

Hello Matlab Community,
I generated the random number of points (200 users) within 7 different circles (different radius) in square box of 1000 by 1000 meters as shown in figure. The number of points (users) in each circle are random but fix. I need to distribute the points (shuffle the users) among the mentioned cirlces for example one cirlce has 68 points (users) and other circle has 35 points (users) in the first iteration in the next iteration these users should be changed (shuffle from one cirlce to other circle) but the total number of points (200 users) among these circles in square box of 1000 by 1000 meters remain same.
Thank You!

9 Comments

Can you provide the code so we can run it?
% Create a random set of coordinates in a circle.
% First define parameters that define the number of points and the circle.
%% Circle_01
n1 = 25;
R1 = 30;
xa = 100; % Center of the circle in the x direction.
yb = 700; % Center of the circle in the y direction.
% Now create the set of points.
t1 = 2*pi*rand(n1,1);
r1 = R1*sqrt(rand(n1,1));
x1 = xa + r1.*cos(t1);
y1 = yb + r1.*sin(t1);
% Now display our random set of points in a figure.
plot(x1,y1,'.', 'MarkerSize', 5)
axis square;
hold on;
grid on;
%% Circle_02
n2 = 35;
R2 = 40;
xc = 450; % Center of the circle in the x direction.
yd = 200; % Center of the circle in the y direction.
% Now create the set of points.
t2 = 2*pi*rand(n2,1);
r2 = R2*sqrt(rand(n2,1));
x2 = xc + r2.*cos(t2);
y2 = yd + r2.*sin(t2);
% Now display our random set of points in a figure.
plot(x2,y2, '.', 'MarkerSize', 5)
axis square;
hold on;
grid on;
%% Circle_03
n3 = 68;
R3 = 50;
xe = 500; % Center of the circle in the x direction.
yf = 600; % Center of the circle in the y direction.
% Now create the set of points.
t3 = 2*pi*rand(n3,1);
r3 = R3*sqrt(rand(n3,1));
x3 = xe + r3.*cos(t3);
y3 = yf + r3.*sin(t3);
% Now display our random set of points in a figure.
plot(x3,y3, '.', 'MarkerSize', 5)
axis square;
hold on;
grid on;
%% Circle_04
n4 = 15;
R4 = 30;
xg = 650; % Center of the circle in the x direction.
yh = 500; % Center of the circle in the y direction.
% Now create the set of points.
t4 = 2*pi*rand(n4,1);
r4 = R4*sqrt(rand(n4,1));
x4 = xg + r4.*cos(t4);
y4 = yh + r4.*sin(t4);
% Now display our random set of points in a figure.
plot(x4,y4, '.', 'MarkerSize', 5)
axis square;
hold on;
grid on;
%% CIrcle_05
n5 = 44;
R5 = 35;
xi = 900; % Center of the circle in the x direction.
yj = 400; % Center of the circle in the y direction.
% Now create the set of points.
t5 = 2*pi*rand(n5,1);
r5 = R5*sqrt(rand(n5,1));
x5 = xi + r5.*cos(t5);
y5 = yj + r5.*sin(t5);
% Now display our random set of points in a figure.
plot(x5,y5, '.', 'MarkerSize', 5)
axis square;
hold on;
grid on;
%% Cirlce_06
n6 = 10;
R6 = 35;
xk = 900; % Center of the circle in the x direction.
yl = 100; % Center of the circle in the y direction.
% Now create the set of points.
t6 = 2*pi*rand(n6,1);
r6 = R6*sqrt(rand(n6,1));
x6 = xk + r6.*cos(t6);
y6 = yl + r6.*sin(t6);
% Now display our random set of points in a figure.
plot(x6,y6, '.', 'MarkerSize', 5)
axis square;
hold on;
grid on;
%% circle_07
n7 = 15;
R7 = 30;
xm = 800; % Center of the circle in the x direction.
yn = 850; % Center of the circle in the y direction.
% Now create the set of points.
t7 = 2*pi*rand(n7,1);
r7 = R7*sqrt(rand(n7,1));
x7 = xm + r7.*cos(t7);
y7 = yn + r7.*sin(t7);
% Now display our random set of points in a figure.
plot(x7,y7, '.', 'MarkerSize', 5)
axis square;
hold on;
grid on;
%% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
axis([0 1000 0 1000])
fontSize = 15;
xlabel('Position X (1000m)', 'FontSize', fontSize);
ylabel('Position Y (1000m)', 'FontSize', fontSize);
title('Random Users Within Circles', 'FontSize', fontSize);
Actually, I am new to matlab so I tried to generate this code.
> ...for example one cirlce has 68 points and other circle has 35 points in the first iteration in the next iteration these users should be changed (shuffle from one cirlce to other circle) but the total number of points (200) remain same.
I understand that there will always be 200 points and I think I understand that there will always be 7 cirlces (correct?). And I think I understand that the cirlce each dot belongs to should randomly change between iterations, right?
What I don't undestand is whether the number of dots per cirlce should be the same between iterations.
@Adam Danz thank you so much for the response.
1) yes, there always be 7 circles.
2) Yes, the cirlce each dot belongs to should randomly change between iterations.
3) No, the number of dots per circle should not be the same between iterations becasue the points (users) move from one circle to other. So, the number of dots per circle should change between the iterations and should not be the same between the iterations.
Hello, @khalid khan. I saw your email asking me to take a look at this problem.
It is a little difficult what exactly you want to be random, and what you want to be constant. The following would be my understanding:
  • Number of users: Constant (200, but notice that your code has 212)
  • Number of circles: Constant (7)
  • (X,Y) center of each circle: Constant? Use the values from your code?
  • Which circle each user "belongs" to: Random, switching every iteration.
  • Color of dot: Corresponds to the user, not the circle where they are located. [Color is only the same in each circle because it is your first iteration?]
  • Radius of each circle: ????
It seems that you want to radius of the circle to be different, depending on how many users are in the circle. Is that right? Can you be more specific? I couldn't see a pattern in your code between radius and user count.
[Sorry if I am repeating some questions from other helpers.]
@the cyclist Thank you so much for the response
1) Yes. Number of users are constant (200)
2) Yes, Number of circles:Constant (7)
3) Yes (X,Y) center of each circle: Constant
4) Random, switching every iteration.
5) Color of dot: Color is should be the same in each circle.
6) Radius of each circle should change based on users mobility
7) Yes exactly, I want the radius of the circle to be different, depending on how many users are in the circle.
@khalid khan, I too received an email. It appears that you are receiving excellent advice from @the cyclist and @Adam Danz and @Riccardo Scorretti, so I don;t think you need me.
I am very thankful to every one for the positive comments and suggestions

Sign in to comment.

 Accepted Answer

There are several ways to do this. Here is one.
I illustrated with 20 users, because with 200 users it is difficult to tell that the number of users per circle is changing.
FYI, with 200 users and 7 circles, if they are assigned randomly (with equal probability), you will get user counts per circle that are pretty close to each other.
% Set the random number generator seed, for reproducibility
rng default
% Number of iterations to run the algorithm
NITER = 5;
% 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 = 20; % <--- 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")
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

More Answers (1)

That's ok?
% Coordinates (x0,y0), radius (rad) and number of users (nbu) for each circle
x0 = [100 450 500 650 900 900 800];
y0 = [700 200 600 500 400 100 850];
rad = [ 30 40 50 30 35 35 30];
nbu = [ 25 35 68 15 44 10 15];
% Generate the users
nbu_tot = sum(nbu);
x = zeros(nbu_tot, 1); % it is better to pre-allocate matrix, when possible
y = zeros(nbu_tot, 1);
grp = zeros(nbu_tot, 1); % group to which each user belongs
base = 0;
for n = 1 : numel(nbu)
ind = base + (1:nbu(n)); % = index of the users
t = 2*pi*rand(nbu(n), 1);
r = rad(n)*sqrt(rand(nbu(n), 1));
x(ind) = x0(n) + r.*cos(t);
y(ind) = y0(n) + r.*sin(t);
grp(ind) = n;
base = base + nbu(n);
end
% Now, just shuffle the groups
grp = grp(randperm(nbu_tot));
% Plot the users
colors = 'rgbkmc'; % many colors ...
figure
for n = 1 : numel(nbu)
t = find(grp == n);
col = colors(1+mod(n,numel(colors)));
plot(x(t), y(t), [col '.'], 'MarkerSize', 5);
hold on
end
axis square ; grid on
It's a little bit different from your original code. The coordinates of all users are in vectors x and y. All users are grouped together; the group to which each user belong is stored in the vector grp.

3 Comments

Thank you!
Actually here I need the mobility of users. for example users of one cirlce should move to another circle and the radius of circle should increase or decrease at each iteration due to the movement of users. let's suppose the users in one cirlce is 25 now some of the these users move from this cirlce and to an other circle due the mobility of the users radius of circle should be dcrease or inecrease at each iteration due to the mobility of users. This is actually my aim
I have two questions about the statement "the users in one cirlce is 25 now some of the these users move from this cirlce and to an other circle due the mobility of the users radius of circle should be dcrease or inecrease at each iteration":
  1. All users are supposed to move at the same time, or one by one?
  2. When user moves away from a circle to another, that circle is required to become smaller. How much smaller?
  3. What happens if, due to shrinking of a circle, some users (which was not supposed to move) find themselves outside of any circle?
  4. What happens if, due to expandinging of a circle, some users (which was not supposed to move) find themselves inside more than a single circle?
Thank you so much for the response
1) Yes, all the users should move at the same time.
2) yes smaller, for example if there are 25 users in circle and 15 users are move to another cirlce so the radius of this circle should reduce based on the users moves from it and radius of other cirlce should increase becasue users move to it.
3) The remaining users should be inside the circle not outside the circle.
4)No, the total numbers of cirlce should remain same 7 circles should be 7 if the users move to it or move from it the total circle should be 7 but the radius should increase or decrease of these circle based on the users move to it or move from it.

Sign in to comment.

Categories

Find more on MATLAB Parallel Server in Help Center and File Exchange

Products

Release

R2021b

Community Treasure Hunt

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

Start Hunting!