How to distribute random number of users within the different circles
2 views (last 30 days)
Show older comments
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!
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/988780/image.png)
9 Comments
William Rose
on 10 May 2022
@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.
Accepted Answer
the cyclist
on 10 May 2022
Edited: the cyclist
on 10 May 2022
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)
Riccardo Scorretti
on 6 May 2022
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
Riccardo Scorretti
on 9 May 2022
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":
- All users are supposed to move at the same time, or one by one?
- When user moves away from a circle to another, that circle is required to become smaller. How much smaller?
- What happens if, due to shrinking of a circle, some users (which was not supposed to move) find themselves outside of any circle?
- 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?
See Also
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!