How to make the postions of each circle random?

1 回表示 (過去 30 日間)
khalid khan
khalid khan 2022 年 5 月 17 日
回答済み: Allen 2022 年 5 月 24 日
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

採用された回答

Mathieu NOE
Mathieu NOE 2022 年 5 月 17 日
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 件のコメント
Khalid Khan
Khalid Khan 2022 年 5 月 18 日
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
Mathieu NOE
Mathieu NOE 2022 年 5 月 18 日
ok no big problem here - don't spend too much time with that !

サインインしてコメントする。

その他の回答 (1 件)

Allen
Allen 2022 年 5 月 24 日
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

カテゴリ

Help Center および File ExchangeInteractive Control and Callbacks についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by