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 CenterFile Exchange에서 Interactive 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