Vectors must be the same lengths. how to solve this error

2 visualizaciones (últimos 30 días)
Catherine tadros
Catherine tadros el 9 de Dic. de 2019
Comentada: Walter Roberson el 9 de Dic. de 2019
clear;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% PARAMETERS %%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Field Dimensions - x and y maximum (in meters)
xm=100;
ym=100;
%x and y Coordinates of the Sink
sink.x=0.5*xm;
sink.y=0.5*ym;
%Number of Nodes in the field
n=100;
%Optimal Election Probability of a node
%to become cluster head
p=0.1;
%Energy Model (all values in Joules)
%Initial Energy
Eo=0.5;
%Eelec=Etx=Erx
ETX=50*0.000000001;
ERX=50*0.000000001;
%Transmit Amplifier types
Efs=10*0.000000000001;
Emp=0.0013*0.000000000001;
%Data Aggregation Energy
EDA=5*0.000000001;
%Values for Hetereogeneity
%Percentage of nodes than are advanced
m=0.1;
%\alpha
a=1;
%maximum number of rounds
rmax=99;
%%%%%%%%%%%%%%%%%%%%%%%%% END OF PARAMETERS %%%%%%%%%%%%%%%%%%%%%%%%
%Computation of do
do=sqrt(Efs/Emp);
%Creation of the random Sensor Network
figure(1);
for i=1:1:n
S(i).xd=rand(1,1)*xm;
XR(i)=S(i).xd;
S(i).yd=rand(1,1)*ym;
YR(i)=S(i).yd;
S(i).G=0;
%initially there are no cluster heads only nodes
S(i).type='N';
temp_rnd0=i;
%Random Election of Normal Nodes
if (temp_rnd0>=m*n+1)
S(i).E=Eo;
S(i).ENERGY=0;
plot(S(i).xd,S(i).yd,'o');
hold on;
end
%Random Election of Advanced Nodes
if (temp_rnd0<m*n+1)
S(i).E=Eo*(1+a)
S(i).ENERGY=1;
plot(S(i).xd,S(i).yd,'+');
hold on;
end
end
S(n+1).xd=sink.x;
S(n+1).yd=sink.y;
plot(S(n+1).xd,S(n+1).yd,'x');
%First Iteration
figure(1);
flag_first_dead=0;
for r=0:1:rmax
r
%Operation for epoch
if(mod(r, round(1/p) )==0)
for i=1:1:n
S(i).G=0;
S(i).cl=0;
end
end
end
hold off;
%Number of dead nodes
dead=0;
%Number of dead Advanced Nodes
dead_a=0;
%Number of dead Normal Nodes
dead_n=0;
%counter for bit transmitted to Bases Station and to Cluster Heads
packets_TO_BS=0;
packets_TO_CH=0;
%counter for bit transmitted to Bases Station and to Cluster Heads
%per round
PACKETS_TO_CH(r+1)=0;
PACKETS_TO_BS(r+1)=0;
figure(1);
for i=1:1:n
%checking if there is a dead node
if (S(i).E<=0)
plot(S(i).xd,S(i).yd,'red .');
dead=dead+1;
if(S(i).ENERGY==1)
dead_a=dead_a+1;
end
if(S(i).ENERGY==0)
dead_n=dead_n+1;
end
hold on;
end
if S(i).E>0
S(i).type='N';
if (S(i).ENERGY==0)
plot(S(i).xd,S(i).yd,'o');
end
if (S(i).ENERGY==1)
plot(S(i).xd,S(i).yd,'+');
end
hold on;
end
end
plot(S(n+1).xd,S(n+1).yd,'x');
STATISTICS(r+1).DEAD=dead;
DEAD(r+1)=dead;
DEAD_N(r+1)=dead_n;
DEAD_A(r+1)=dead_a;
%When the first node dies
if (dead==1)
if(flag_first_dead==0)
first_dead=r
flag_first_dead=1;
end
end
for i=1:1:n
if(S(i).E>0)
temp_rand=rand;
if ( (S(i).G)<=0)
%Election of Cluster Heads
hold on;
S(i).xd=rand(1,n)*xm;
S(i).yd=rand(1,n)*ym;
k=100;
CHx=kmeans(S(i).xd,k);
CHy=kmeans(S(i).yd,k);
plot(CHx,CHy,'r*')
packets_TO_BS=packets_TO_BS+1;
PACKETS_TO_BS(r+1)=packets_TO_BS;
distance=sqrt( (S(i).xd-(S(n+1).xd) ).^2 + (S(i).yd-(S(n+1).yd) ).^2 );
%Calculation of Energy dissipated
distance;
if (distance>do)
S(i).E=S(i).E- ( (ETX+EDA).*(4000) + Emp.*4000.*( distance.*distance.*distance.*distance ));
end
if (distance<=do)
S(i).E=S(i).E- ( (ETX+EDA).*(4000) + Efs.*4000.*( distance .* distance ));
end
end
end
sum=0;
for i=1:1:n
if(S(i).E>0)
sum=sum+S(i).E;
end
end
avg=sum/n;
STATISTICS(r+1).AVG=avg;
sum;
end
figure(2);
for r=0:1:99
ylabel('Average Energy of Each Node');
xlabel('Round Number');
plot([r r+1],[STATISTICS(r+1).AVG STATISTICS(r+2).AVG],'blue');
hold on;
end
figure(3);
for r=0:1:249
ylabel('Number of Dead Nodes');
xlabel('Round Number');
plot([r r+1],[STATISTICS(r+1).DEAD STATISTICS(r+2).DEAD],'blue');
hold on;
end
Vectors must be the same lengths.

Respuestas (1)

Walter Roberson
Walter Roberson el 9 de Dic. de 2019
STATISTICS(r+1).AVG=avg;
r is not changing inside that loop, so you are always overwriting the same location, and all of the other ones come out empty.

Categorías

Más información sobre Quadratic Programming and Cone Programming en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by