Info

This question is closed. Reopen it to edit or answer.

Unable to perform the assignment because the left & the right side have a different number of elements

1 view (last 30 days)
I'm getting the error as stated in the title. I can't figure out why. We should be able to assign scalar values to array elements like a[0]=2,a[5]=5, etc
So why is it giving me an arror in the line : average=avg
attached is the full code for your reference:
clear;
close all;
clc;
ad=-99;
% PARAMETER initialization
%Number of Nodes in the field
% n=200;
fd=-99;
n=300;
%Energy Model (all values in Joules)
%Initial Energy
Eo=0.5;
%Field Dimensions - x and y maximum (in meters)
% xm=input('Enter x value for area plot : ');
% ym=input('Enter y value for area plot : ');
xm=100;
ym=100;
%message size in bits
l=2000;
%x and y Coordinates of the Sink
sink.x=0.5*xm;
sink.y=0.5*ym;
%Optimal Election Probability of a node
%to become cluster head
p=0.1;
ETX=50*0.000000001;
ERX=50*0.000000001;
Eelec=ETX;
%Transmit Amplifier types
Efs=10*0.000000000001;
Emp=0.0013*0.000000000001;
%Data Aggregation Energy
EDA=5*0.000000001;
%Values for Hetereogeneity
%fraction of nodes than are advanced
m=0.1;
%alpha
a=2;
%beta
b=3;
flag_ad=0;
%maximum number of rounds
rmax=300;
CLUSTERHS=zeros(rmax);
%optimal number of cluster heads
k=30;% optimal number of cluster heads = 0.1 of 300 nodes
rcountCHs=zeros(rmax);
%Computation of do
do=sqrt(Efs/Emp);
%Creation of the random Sensor Network
figure(1);
plot(sink.x,sink.y,'X');
%creating the cluster head threshold array
thresh=zeros(1,n);
%creating a cluster head probability array
P=zeros(1,n);
hold off;
t=0;
S(n+1).xd=sink.x;
S(n+1).yd=sink.y;
S(1).E=Eo*(n*(1-m)+n*m*(1+b));
x1=40;
y1=40;
x2=60;
y2=60;
a2=100;
b2=100;
%initializing the balanced influential factor
psi=zeros(n,rmax);
%distance to the base station for all nodes in WSN
d=zeros(n);
%Optimal Election Probability of a node
%to become cluster head taken to be 10%
prob=0.1;
%sink placed at the center of the field
S(n+1).xd=0.5*100;
S(n+1).yd=0.5*100;
%number of normal and advanced nodes
n_normal=0;
n_advanced=0;
%C.d=zeros(n);
n_alive=0;
Energy_disp=zeros(1,rmax);
%avgdist=0;
first=0;
h(1).xd=25;%helper node 1 x coordinate
h(1).yd=45;%helper node 1 y coordinate
h(1).E=Eo*(1+a);%initial energy of the helper node
h(2).xd=75;%helper node 2 x coordinate
h(2).yd=45;%helper node 2 y coordinate
h(2).E=Eo*(1+a);
h(3).xd=25;%helper node 3 x coordinate
h(3).yd=55;%helper node 3 y coordinate
h(3).E=Eo*(1+a);
h(4).xd=75;%helper node 4 x coordinate
h(4).yd=55;%helper node 4 y coordinate
for i=1:1:n/4
C(i).id=1;
S(i).xd=rand(1,1)*x1;
S(i).yd=rand(1,1)*y1;
S(i).d=sqrt( (S(i).xd-(h(1).xd) )^2 + (S(i).yd-(h(1).yd) )^2 );%distance to helper node
S(i+n/4).d=sqrt( (S(i+n/4).xd-(h(2).xd) )^2 + (S(i+n/4).yd-(h(2).yd) )^2 );
S(i+n/2).d=sqrt( (S(i+n/2).xd-(h(3).xd) )^2 + (S(i+n/2).yd-(h(3).yd) )^2 );
S(i+3*n/4).d=sqrt( (S(i+3*n/4).xd-(h(4).xd) )^2 + (S(i+3*n/4).yd-(h(4).yd) )^2 );
XR(i)=S(i).xd;
YR(i)=S(i).yd;
plot(S(i).xd,S(i).yd,'s');
xlim([0 100]);
ylim([0 100]);
S(i).G=0;
S(i).type='N';
C(i+n/4).id=2;
S(i+n/4).xd=(a2-x2).*rand(1,1)+x2;
S(i+n/4).yd=(b2-y2).*rand(1,1)+y2;
plot(S(i+n/4).xd,S(i+n/4).yd,'s');
xlim([0 100]);
ylim([0 100]);
S(i+n/4).G=0;
S(i+n/4).type='N';
S(i+n/2).xd=(a2-x2).*rand(1,1)+x2;
S(i+n/2).yd=y1*rand(1,1);
plot(S(i+n/2).xd,S(i+n/2).yd,'s');
xlim([0 100]);
ylim([0 100]);
C(i+n/2).id=4;
S(i+3*n/4).G=0;
S(i+3*n/4).type='N';
S(i+3*n/4).xd=x2*rand(1,1);
S(i+3*n/4).yd=(b2-y2).*rand(1,1)+y2;
C(i+3*n/4).id=3;
plot(S(i+3*n/4).xd,S(i+3*n/4).yd,'s');
plot(S(n+1).xd,S(n+1).yd,'x');%base station
xlim([0 100]);
ylim([0 100]);
S(i+3*n/4).G=0;
S(i+3*n/4).type='N';
hold on;
%Random Election of Normal Nodes
if(n_normal<(n-(m*n)))
S(i).type='N';
S(i).E=Eo;
plot(S(i).xd,S(i).yd,'s');
S(i+n/4).type='N';
S(i+n/4).E=Eo;
plot(S(i+n/4).xd,S(i+n/4).yd,'s');
n_normal=n_normal+1;
S(i+n/2).type='N';
S(i+n/2).E=Eo;
n_normal=n_normal+1;
plot(S(i+n/2).xd,S(i+n/2).yd,'s');
S(i+3*n/4).type='N';
S(i+3*n/4).E=Eo;
n_normal=n_normal+1;
plot(S(i+3*n/4).xd,S(i+3*n/4).yd,'s');
hold on;
end
if(n_advanced<n*m+1)
S(i).type='A';
S(i).E=Eo*(1+a);
S(i).Energy=1;
n_advanced=n_advanced+1;
plot(S(i).xd,S(i).yd,'+');
S(i+n/4).type='A';
S(i+n/4).E=Eo*(1+a);
S(i+n/4).Energy=1;
n_advanced=n_advanced+1;
plot(S(i+n/4).xd,S(i+n/4).yd,'+');
S(i+n/2).type='A';
S(i+n/2).E=Eo*(1+a);
S(i+n/2).Energy=1;
n_advanced=n_advanced+1;
plot(S(i+n/2).xd,S(i+n/2).yd,'+');
S(i+3*n/4).type='A';
S(i+3*n/4).E=Eo*(1+a);
S(i+3*n/4).Energy=1;
n_advanced=n_advanced+1;
plot(S(i+3*n/4).xd,S(i+3*n/4).yd,'+');
hold on;
end
end
h(1).d=sqrt( (S(n+1).xd-(h(1).xd) )^2 + (S(n+1).yd-(h(1).yd) )^2 );%distance from the helper node 1
h(2).d=sqrt( (S(n+1).xd-(h(2).xd) )^2 + (S(n+1).yd-(h(2).yd) )^2 );%distance from the helper node 2
h(3).d=sqrt( (S(n+1).xd-(h(3).xd) )^2 + (S(n+1).yd-(h(3).yd) )^2 );%distance from the helper node 3
h(4).d=sqrt( (S(n+1).xd-(h(4).xd) )^2 + (S(n+1).yd-(h(4).yd) )^2 );%distance from the helper node 4
plot(h(1).xd,h(1).yd,'x');
plot(h(2).xd,h(2).yd,'x');
plot(h(3).xd,h(3).yd,'x');
plot(h(4).xd,h(4).yd,'x');
h(1).E=Eo*(1+a);
h(2).E=Eo*(1+a);
h(3).E=Eo*(1+a);
h(4).E=Eo*(1+a);
h(1).type='A';
h(2).type='A';
h(3).type='A';
%flag_fd=0;
flag_hd=0;
hd1=-999;
average=zeros(1,rmax);
davgbs=0;
%plot(S(n+1).xd,S(n+1).yd,'o', 'MarkerSize', 12, 'MarkerFaceColor', 'r');
figure(1);
% figure(1)
%plot(o1,o2,'^','LineWidth',1, 'MarkerEdgeColor','k', 'MarkerFaceColor','y', 'MarkerSize',12);
% hold on
%First Iteration
%counter for CHs
countCHs=0;
%counter for CHs per round
%rcountCHs=0;
cluster=1;
%countCHs;
%rcountCHs=rcountCHs+countCHs;
countCHs=0;
%counter for CHs per round
%rcountCHs=0;
countCHs;
flag_first_dead=0;
%throughput=zeros(rmax);
for i=1:1:n
davgbs=davgbs+S(i).d;
end
dvgbs=davgbs/n;
z=0;%total dead nodes
%sum=zeros(1,rmax);
for r1=1:1:rmax
r1
temp_rn=rand;
%sum=0;
%t initialised to zero after every round
t=0;
%Operation for epoch
if(mod(r1, round(1/prob) )==0)
for i=1:1:n
S(i).G=0;
S(i).cl=0;
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 Base 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(r1)=0;
PACKETS_TO_BS(r1)=0;
%initialising countCHs to 0 per round
countCHs = 0;
figure(1);
for i=1:1:n
if (S(i).d<do)
S(i).E=S(i).E- ( (Eelec)*(l) + Emp*l*( d*d));
%Energy_disp(r1)=Energy_disp(r1)+(Eelec)*(l) + Emp*l*( d*d);
%S(i).E=S(i).E- ( (ETX+EDA)*(4000) + Emp*4000*( distance*distance*distance*distance ));
end
if (S(i).d>=do)
S(i).E=S(i).E- ( (ETX+EDA)*(4000) + Efs*4000*( d * d * d * d ));
% Energy_disp(r1)=Energy_disp(r1)+(ETX+EDA)*(4000) + Efs*4000*( d * d * d * d );
%S(i).E=S(i).E- ( (ETX+EDA)*(4000) + Efs*4000*( distance * distance ));
end
%checking if there is a dead node
if (S(i).E<=0)
plot(S(i).xd,S(i).yd,'^','LineWidth',1, 'MarkerEdgeColor','k', 'MarkerFaceColor','y', 'MarkerSize',8);
dead=dead+1;%dead nodes per round
z=z+1;%total dead nodes
S(i).E=0;
S(i).type='d';
if (dead==1 && z==1)
if(flag_first_dead==0)
fd=r1;
flag_first_dead=1;
end
end
if(S(i).E==1)
dead_a=dead_a+1;
end
if(z==0.5*n)
if(flag_hd==0)
hd1=r1;
flag_hd=1;
end
end
if(S(i).E==0)
dead_n=dead_n+1;
end
hold on;
end
if(z==n)
if(flag_ad==0)
flag_ad=1;
ad1=r1;
end
end
if S(i).E>0
n_alive=n_alive+1;
S(i).type='N';
if (S(i).E==0)
plot(S(i).xd,S(i).yd,'s','LineWidth',1, 'MarkerEdgeColor','k', 'MarkerFaceColor','g', 'MarkerSize',8);
end
if (S(i).E==1)
plot(S(i).xd,S(i).yd,'+','LineWidth',3, 'MarkerEdgeColor','k', 'MarkerFaceColor','r', 'MarkerSize',8);
% ask Shreyas about this
end
hold on;
end
end
%plot(S(n+1).xd,S(n+1).yd,'x','LineWidth',1, 'MarkerEdgeColor','k', 'MarkerFaceColor','r', 'MarkerSize',8);
STATISTIC(r1).DEAD=dead;
%DEAD(r1)=dead;
DEAD_N(r1)=dead_n;
DEAD_A(r1)=dead_a;
% plot(S(n+1).xd,S(n+1).yd,'o', 'MarkerSize', 12, 'MarkerFaceColor', 'r');
% plot(S(n+1).xd,S(n+1).yd,'x','LineWidth',1, 'MarkerEdgeColor','k', 'MarkerFaceColor','r', 'MarkerSize',8);
%When the first node dies
countCHs=0;
cluster=1;
for i=1:1:n
if(S(i).E>0)
temp_rand=rand;
t=t+1;
if ( (S(i).G)<=0)
%if the G value is -ve
%Election of Cluster Heads according to the flowchart given in the paper
if(S(i).type~='d')
if(t<k && countCHs<k)
if(t>0)
while(rcountCHs(r1)<t)
% if(S(i).type=='A')
rcountCHs(r1)=rcountCHs(r1)+1;
packets_TO_BS=packets_TO_BS+1;
PACKETS_TO_BS(r1)=packets_TO_BS;
cluster=cluster+1;
plot(S(i).xd,S(i).yd,'dr');%plots cluster heads with red diamonds
S(i).type='C';
S(i).G=round(1/prob)-1;
C(cluster).xd=S(i).xd;
C(cluster).yd=S(i).yd;
d=sqrt( (S(i).xd-(S(n+1).xd) )^2 + (S(i).yd-(S(n+1).yd) )^2 );
C(cluster).distance=d;
C(cluster).id=i;
X(cluster)=S(i).xd;
Y(cluster)=S(i).yd;
%end
end
%ask help in implementing this flowchart-selection of cluster heads
if(t==0 & S(i).type~='C')
S(i).G=round(1/p)-1;
cluster=cluster+1;
end
%electing k-t cluster heads
while(rcountCHs(r1)<k-t)
rcountCHs(r1)=rcountCHs(r1)+1;
packets_TO_BS=packets_TO_BS+1;
PACKETS_TO_BS(r1)=packets_TO_BS;
S(i).type='C';
S(i).G=round(1/prob)-1;
cluster=cluster+1;
C(cluster).xd=S(i).xd;
C(cluster).yd=S(i).yd;
plot(S(i).xd,S(i).yd,'dr');
d=sqrt( (S(i).xd-(S(n+1).xd) )^2 + (S(i).yd-(S(n+1).yd) )^2 );
C(cluster).distance=d;
C(cluster).id=i;
X(cluster)=S(i).xd;
Y(cluster)=S(i).yd;
end
end
else
while(rcountCHs(r1)<t)
rcountCHs(r1)=rcountCHs(r1)+1;
packets_TO_BS=packets_TO_BS+1;
PACKETS_TO_BS(r1)=packets_TO_BS;
S(i).type='C';
S(i).G=round(1/p)-1;%UPDATING THE G VALUES OF THE NODES
cluster=cluster+1;
plot(S(i).xd,S(i).yd,'dr');%plots cluster heads with red diamonds
C(cluster).xd=S(i).xd;
C(cluster).yd=S(i).yd;
%plot(S(i).xd,S(i).yd,'k*');
d=sqrt( (S(i).xd-(S(n+1).xd) )^2 + (S(i).yd-(S(n+1).yd) )^2 );
C(cluster).distance=d;
C(cluster).id=i;
X(cluster)=S(i).xd;
Y(cluster)=S(i).yd;
end
%rcountCHs(r1)=rcountCHs+countCHs;
%plot(r,countCHs);
%Calculation of Energy dissipated & residual energy according to the energy
%equations in the paper from the helper nodes to the CH
for iter=1:1:4
if (h(iter).d<do)
S(i).E=S(i).E- ( (Eelec)*(l) + Emp*l*( d*d));
%Energy_disp(r1)=Energy_disp(r1)+(Eelec)*(l) + Emp*l*( d*d);
%S(i).E=S(i).E- ( (ETX+EDA)*(4000) + Emp*4000*( distance*distance*distance*distance ));
end
if (h(iter).d>=do)
S(i).E=S(i).E- ( (ETX+EDA)*(4000) + Efs*4000*( d * d * d * d ));
% Energy_disp(r1)=Energy_disp(r1)+(ETX+EDA)*(4000) + Efs*4000*( d * d * d * d );
%S(i).E=S(i).E- ( (ETX+EDA)*(4000) + Efs*4000*( distance * distance ));
end
end
end
end
end
end
if(S(i).E<0)
rcountCHs(r1)=0;
end
end
STATISTIC(r1).CLUSTERHEADS=cluster-1;
CLUSTERHS(r1)=cluster-1;
%Election of Associated Cluster Head for Normal Nodes
for i=1:1:n
if ( S(i).type=='N' & S(i).E>0 )
if(cluster-1>=1)
min_dis=sqrt( (S(i).xd-S(n+1).xd)^2 + (S(i).yd-S(n+1).yd)^2 );
min_dis_cluster=1;
for c=1:1:cluster-1
temp=min(min_dis,sqrt( (S(i).xd-C(c).xd)^2 + (S(i).yd-C(c).yd)^2 ) );
if ( temp<min_dis )
min_dis=temp;
min_dis_cluster=c;
end
end
% figure(4);
% title('Count of cluster heads per round');
% xlabel('round');
% ylabel('count of cluster heads');
% plot(r,countCHs);
%Energy dissipated by associated Cluster Head
if(min_dis_cluster~=0)
min_dis;
if (min_dis>do)
S(i).E=S(i).E- ( ETX*(l) + Efs*l*( min_dis * min_dis));
end
if (min_dis<=do)
S(i).E=S(i).E- ( ETX*(l) + Emp*4000*( min_dis * min_dis * min_dis * min_dis));
end
S(C(min_dis_cluster).id).E = S(C(min_dis_cluster).id).E- ( (ERX + EDA)*4000 );
packets_TO_CH=packets_TO_CH+1;
%Energy dissipated
if(min_dis>0)
d=sqrt( (S(C(min_dis_cluster).id).xd-(S(n+1).xd) )^2 + (S(C(min_dis_cluster).id).yd-(S(n+1).yd) )^2 );
S(C(min_dis_cluster).id).E = S(C(min_dis_cluster).id).E- ( (ERX + EDA)*4000 );
if (d>do)
S(C(min_dis_cluster).id).E=S(C(min_dis_cluster).id).E- ( (ETX+EDA)*(4000) + Emp*4000*( d*d*d*d ));
end
if (d<=do)
S(C(min_dis_cluster).id).E=S(C(min_dis_cluster).id).E- ( (ETX+EDA)*(4000) + Efs*4000*( d * d ));
end
PACKETS_TO_CH(r1)=packets_TO_CH;
end
S(i).min_dis=min_dis;
S(i).min_dis_cluster=min_dis_cluster;
end
end
end
end
hold on;
countCHs;
rcountCHs=rcountCHs+countCHs;
sum=0;
for i=1:1:n
if(S(i).E>0)
sum=sum+S(i).E;
end
end
avg=sum/n-Eo;
average(r1)=avg;
Even when I remove the avg variable & do sum/n-Eo directly the error persists. I am at my wits end. what to do? how do I get rid of this error?
Scalars being assigned to array elements is nothing new. why an error for this? What does the error message exactly mean "left & right side have a different number of elements"in terms of the error I'm getting? Kindly shed some light on what seems to be so dark.
  3 Comments
Image Analyst
Image Analyst on 12 Mar 2019
Edited: Image Analyst on 12 Mar 2019
Just attach the actual m-file with the paper clip icon. Then delete all the code from your answer (it's no good anyway according to Jan), or else format it by highlighting and clicking the Code icon Code can be indented properly in the MATLAB editor by typing control-a control-i.
Then if you still have errors after fixing the sum variable name problem Jan pointed out, give us ALL THE RED TEXT, which includes the line of text, the line number, etc. Not just a snippet of the red text but ALL of it.

Answers (0)

This question is closed.

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!