如何在matlab中打印每轮后的变量值?
我有100个节点使用以下for循环:如何在matlab中打印每轮后的变量值?,matlab,for-loop,Matlab,For Loop,我有100个节点使用以下for循环: xm1=100; %diameters of sensor network ym1=100; sink1.x=50; %distance of base station from the network sink1.y=120; nl = 100; %no of nodes pl=0.1; %probibilty of a node to become cluster head El1= 0; Eint=0
xm1=100; %diameters of sensor network
ym1=100;
sink1.x=50; %distance of base station from the network
sink1.y=120;
nl = 100; %no of nodes
pl=0.1; %probibilty of a node to become cluster head
El1= 0;
Eint=0.5; %energy supplied to each node
Etl =0 ;
for i=1:1:nl
Sl(i).xd=rand(1,1)*xm1; %it will distribute the nodes in 1 dimension in x axis randomly.
XR(i)=Sl(i).xd; %we store its value in xr
Sl(i).yd=rand(1,1)*ym1; %it will distribute the nodes in 1 dimension in y axis randomly
YR(i)=Sl(i).yd;
Sl(i).id = i;
Sl(i).G=0; % as the no of node that have been cluster head is zero 0
Sl(i).E=Eint; %%*(1+rand*a);
%fprintf(' Sensor node energy: %15f \n',Eint); %?
%initially there are no cluster heads only nodes
Sl(i).type='N';
end
团簇的形成类似tis
for r=1:1:rmax
for i=1:1:nl
if(Sl(i).E>0)
temp_randl=rand;
if ( (Sl(i).G)<=0)
if(temp_randl<= (pl/(1-pl*mod(r,round(1/pl)))))
countCHsl=countCHsl+1;
packets_TO_BSl=packets_TO_BSl+1;
PACKETS_TO_BSl(r+1)=packets_TO_BSl;
Sl(i).type='C';
Sl(i).G=round(1/pl)-1;
Cl(clusterl).xd=Sl(i).xd;
Cl(clusterl).yd=Sl(i).yd;
distancel=sqrt( (Sl(i).xd-(Sl(n+1).xd) )^2 + (Sl(i).yd-(Sl(n+1).yd) )^2 );
Cl(clusterl).distance=distancel;
Cl(clusterl).id=i;
X(clusterl)=Sl(i).xd;
Y(clusterl)=Sl(i).yd;
clusterl=clusterl+1;
distancel;
if (distancel>do)
Sl(i).E=Sl(i).E- ( (ETX+EDA)*(4000) + Emp*4000*(distancel*distancel*distancel*distancel ));
end
if (distancel<=do)
Sl(i).E=Sl(i).E- ( (ETX+EDA)*(4000) + Efs*4000*(distancel * distancel ));
end
end
end
% S(i).G=S(i).G-1;
end
end
STATISTICS.COUNTCHS(r+1)=countCHsl;
for i=1:1:nl
if ( Sl(i).type=='N' && Sl(i).E>0 )
if(clusterl-1>=1)
min_disl=Inf;
min_dis_clusterl=0;
for cl=1:1:clusterl-1
templ=min(min_disl,sqrt( (Sl(i).xd-Cl(cl).xd)^2 + (Sl(i).yd-Cl(cl).yd)^2 ) );
if ( templ<min_disl )
min_disl=templ;
min_dis_clusterl=cl;
end
end
min_disl;
if (min_disl>do)
Sl(i).E=Sl(i).E- ( ETX*(4000) + Emp*4000*( min_disl *min_disl * min_disl * min_disl));
end
if (min_disl<=do)
Sl(i).E=Sl(i).E- ( ETX*(4000) + Efs*4000*( min_disl * min_disl));
end
Sl(Cl(min_dis_clusterl).id).E =Sl(Cl(min_dis_clusterl).id).E- ( (ERX + EDA)*4000 );
packets_TO_CHl=packets_TO_CHl+1;
Sl(i).min_disl=min_disl;
Sl(i).min_dis_clusterl=min_dis_clusterl;
else
min_disl=sqrt( (Sl(i).xd-Sl(n+1).xd)^2 + (Sl(i).yd-Sl(n+1).yd)^2 );
if (min_disl>do)
Sl(i).E=Sl(i).E- ( ETX*(4000) + Emp*4000*( min_dis1 *min_dis1 * min_dis1 * min_dis1));
end
if (min_disl<=do)
Sl(i).E=Sl(i).E- ( ETX*(4000) + Efs*4000*( min_disl * min_disl));
end
packets_TO_BSl=packets_TO_BSl+1;
end
end
end
end
简单的
S1(i.E.),
应该行。我假设r代表圆形,所以您可以在最后一个端点之前添加以下任何一行来打印剩余能量:
- sprintf('%0.2f\n',Sl(:).E)李>
- disp([Sl(:).E]')李>
- Sl(:).E
fprintf('%0.2f \n',Sl(:).E);