how to run written code for 1000 times by for loop i addition to inner for loops inside code?

6 Ansichten (letzte 30 Tage)
i have written my code but i need to run it for 1000 times because i need to take mean of some variables such as Qfog after 1000 times because i used many rand & randi in my code, how can i do this in presecence og pre-allocated variables. the code is attached
clear
Nu=(1:1:10); NF=(1:1:4); Zu=(1:1:10); Cf=10^6*[4 10 15 7]; % different computing capacity on eacg fog
yt=0.1; ye=0.1; delta=0.05;
%wireless interference model Rayleigh
W =10*10^6;
% from file new try.m
N = 10; % number of users distributed in the ciurcle
R = 100;
C=[40 40]; % center of circle
%x0 = [200 400 0 400] ; % Center of the circle in the x direction.
%y0= [200 200 200 0]; % Center of the circle in the y direction.
%generate the circle boundary
t = linspace(0, 2*pi, 50);
x = R*cos(t) + C(1);
y = R*sin(t) + C(2);
% generate random points / users inside
th = 2*pi*rand(N,1);
r = R*rand(N,1);
xR = r.*cos(th) + C(1);
yR = r.*sin(th) + C(2);
Xf=[20 40 0 40];Yf=[20 20 20 0];
% finding distance between each random point xR,yR and centers of fogs Xf,Yf
x_coor=zeros(10,4);y_coor=zeros(10,4);
for i=1:4%columns
Xf(1,i)=Xf(1,i);
Yf(1,i)=Yf(1,i);
x_coor(:,i)=xR(:,1)-Xf(1,i);
y_coor(:,i)=yR(:,1)-Yf(1,i);
end
d=sqrt(x_coor.^2+y_coor.^2);% distance between fog & user
n=1000;
result=zeros(n,1);
for p=1:n
%%generating average sojourn time in 4 fogs
mu=30;var=100;
%rng (1,'twister')
ts=mu+sqrt(var)*randn(10,4);
Du=randi([10*10^6,15*10^6],10,1);
fu=randi([2*10^9,5*10^9],10,1);
Culocal=randi([0.5*10^9,0.8*10^9],10,1);
Tulocal=fu./Culocal;
k=10^-26;
Elocal=k*Culocal'.^2*fu;
Qlocal=0.1.*Elocal+0.1.*Tulocal;
SINR_dB=randi([-2,2],10,4);
SINR=10.^(SINR_dB./10);
Ruf=10*10^6*(log2(1.+SINR));
tup=zeros(10,4); texe=zeros(10,4); %%tup = Du./Ruf;
for rr=1:10 %calculating tup for each fog based on Du of 10 UEs (j=num of fog)
tup(rr,:)=Du(rr,1)./Ruf(rr,:);
texe(rr,:)=fu(rr,1)./Cf(1,:);
end
Tuf=texe+tup; %computing time on fog Tuf=texe+tup.%texe=fu./Cf;
pu=0.1; Eup=tup*pu; % calculating uplink energy.
%%%%%calculating mean Q on fog.
Qmig=delta.*Du;
part1=1-exp(-Tuf./ts);
%% for loop to get Qfog due to dimensiion
Qfog=zeros(10,4); revenu=zeros(10,4);
for v=1:10 %%rows of num. of users
Qmig(:,1)=Qmig(:,1);
Qfog(v,:)=(Qmig(v,1).*part1(v,:))+(yt.*Tuf(v,:)+ye.*Eup(v,:)); %Qfog=(Qmig*(1-exp(-Tuf'/ts')))+yt.*Tuf+ye.*Eup;
revenu(v,:)=Qlocal(v,1)-Qfog(v,:); %revenu=(Qlocal-Qfog);
end
result(p)=mean(Qfog);
end
%revenu1=max(revenu);
%plot(Nu,revenu1)
%% algorithm 1 steps
Qlocal_rep=repmat(Qlocal,1,4); %repeat dimension for comparison in Bf
%Bf=find((tup<ts)&(Qfog<Qlocal_rep));% gives indices in double format
%Nu1=Nu(Bf);
Bf=zeros(10,4);
for xx=1:10
for yy=1:4
if Qfog(xx,yy)<Qlocal_rep(xx,yy);
Bf(xx,yy)=1;
else
Bf(xx,yy)=0;
end
end
end
cc=Qlocal_rep-Qfog;
  2 Kommentare
Julius Muschaweck
Julius Muschaweck am 28 Aug. 2021
You could just enclose your complete code into a function, returning the results you're interested in, and run that function 1000 times? Your preallocated variables will be local within the function, and the rand and randi functions will return different random numbers in each call.
Like
someseedvalue = 42;
rng(someseedvalue);
for i =1:1000
test(i) = tmp();
end
result = mean(test)
result = 4.9026
function rv = tmp()
a = 10;
rv = a * rand(1);
end
The call to rng (init random number generator) ensures you get reproducible results. If you omit this call, you will get different result each time you run the loop.
BTW: Your way of creating random user locations does not equidistribute them inside the circle. Your users will crowd around the circle center. In your particular case, you could achieve equidistributed users by replacing
r = R*rand(N,1);
with
r = R*sqrt(rand(N,1));
heba raouf
heba raouf am 30 Aug. 2021
thanks sir -- i will try to learn converting code to function and tell u if there any problem

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Pritam Das
Pritam Das am 28 Aug. 2021
You can run an another for loop to get the 1000 values of your desired variable. Put a for loop for the whole code that you want to run for thousant times and put index on the variables you need. suppose you need 1000 values of 'cc'.
for i=1:1000
...Codes...
cc(i)=Qlocal_rep-Qfog;
end
  3 Kommentare
heba raouf
heba raouf am 30 Aug. 2021
i tried your solution sir and this is the error message:
In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in smal_scale_1000 (line 86)
result(p)=mean(Qfog);
i dont understand why?
can u tell me ?
thanks for your effort
Pritam Das
Pritam Das am 30 Aug. 2021
If you pass index to variable A i.e. A(I), number of elements in 'A' will be equal to 'I'. If you want 1000 values in 'A' then only do A(I). And to avoid dimension error you can initialize the size of A.
As Qfog is a matrix mean(Qfog) will give you a row vector. Use the output as 'result(P,:)'.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by