how to store all values in one matrix in my code ?

here start's my code see below and run my code after running my code you will get like this
r =
0
All cluster head:
X Y E D
20.8804 50.6459 0.9994 93.2509
66.3680 104.8504 0.9997 33.9800
80.0382 156.6456 0.9996 60.0599
84.6183 125.4252 0.9997 29.7159
84.7459 92.5260 0.9998 16.9867
121.3182 55.9365 0.9997 48.9496
135.9162 22.4920 0.9995 85.4252
147.9776 50.8509 0.9996 68.6840
178.1875 56.5368 0.9994 89.4558
183.0381 54.4672 0.9994 94.7025
189.6740 177.5828 0.9988 118.5770
199.6349 138.7574 0.9991 106.9076
up to r=5, and now i want to access all the values in one matrix like this
AllCH111(1).x1=[135.9162 22.4920 0.9995 85.4252
147.9776 50.8509 0.9996 68.6840
178.1875 56.5368 0.9994 89.4558
183.0381 54.4672 0.9994 94.7025
189.6740 177.5828 0.9988 118.5770
199.6349 138.7574 0.9991 106.9076];
AllCH111(2).x1=[135.9162 22.4920 0.9995 85.4252
147.9776 50.8509 0.9996 68.6840
178.1875 56.5368 0.9994 89.4558
183.0381 54.4672 0.9994 94.7025
189.6740 177.5828 0.9988 118.5770
199.6349 138.7574 0.9991 106.9076];
AllCH111(3).x1=[135.9162 22.4920 0.9995 85.4252
147.9776 50.8509 0.9996 68.6840
178.1875 56.5368 0.9994 89.4558
183.0381 54.4672 0.9994 94.7025
189.6740 177.5828 0.9988 118.5770
199.6349 138.7574 0.9991 106.9076];
like this want to get..and i have tried in my code it's self see last 'for loop'

 Réponse acceptée

The last for loop of your code is:
for r=1:rmax
AllCH111(r).x1=AllCH1(:,1);
AllCH112(r).y1=AllCH1(:,2);
CHbest1(r).x1=CHbest(:,1);
CHbest2(r).y1=CHbest(:,2);
end
In this loop AllCH1(:,1) is not varying, you need to put this inside the loop where ALLCH1 is varying

7 commentaires

Matlab111
Matlab111 le 4 Déc 2014
no sir, i'm getting the correct answer same problem.
what do you mean?
Matlab111
Matlab111 le 4 Déc 2014
i tried, putting this in inside loop 'AllCH111(r).x1=AllCH1(:,1);' i'm not getting the answer
I said, put it in the other loop, where AllCH1 is varying. In the last loop AllCH1(:,1) is not varying!
clc;
clear
close all;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%PARAMETERS %%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Number of Nodes in the field
n=100;
%n=input('Enter the number of nodes in the space : ');
%Energy Model (all values in Joules)
%Initial Energy
Eo=1;
%S.E=5;
%Eo=input('Enter the initial energy of sensor nJ : ');
%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=200;
ym=200;
%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;
%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.5;
%\alpha
a=0.5;
%beta
b=0.5;
%maximum number of rounds
%rmax=input('enter the number of iterations you want to run : ');
rmax=5; %New Metaheuristic Bat-Inspired Algorithm;
%%%%%%%%%%%%%%%%%%%%%%%%%END OF PARAMETERS %%%%%%%%%%%%%%%%%%%%%%%%
%Computation of do
do=sqrt(Efs/Emp);
%Creation of the random Sensor Network
figure(1);
hold off;
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';
S(i).E=Eo;
S(i).ENERGY=0;
plot(S(i).xd,S(i).yd,'o');
hold on;
end
S(n+1).xd=sink.x;
S(n+1).yd=sink.y;
%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;
flag_first_dead=0;
dead1=0;
dead2=0;
b = []; %# Modification
for r=0:1:rmax
%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
hold off;
%Number of dead nodes
dead=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)
if(dead1==0)
dead1=0;
%disp(dead1);
disp('1st node dead');
dead1=[S(i).xd,S(i).yd];
disp(dead1);
end
if(r==1)
if (S(i).E<=0)
dead2=[S(i).xd,S(i).yd];
disp('2st node dead');
c1 = dead2(~ismember(dead2, dead1));
disp(c1);
end
end
if(r==2)
if (S(i).E<=0)
dead3=[S(i).xd,S(i).yd];
disp('3st node dead');
c2 = dead3(~ismember(dead3, dead2));
disp(c2);
end
end
% disp('Next cluster head');
% CH_111=[X(cluster),Y(cluster)];
% disp(CH_111);
plot(S(i).xd,S(i).yd,'*r');
%,'LineWidth',1, 'MarkerEdgeColor','k', 'MarkerFaceColor','y', 'MarkerSize',8);
dead=dead+1;
hold on;
end
%dead2=[S(i-1).xd,S(i-1).yd];
if S(i).E>0
S(i).type='N';
if (S(i).ENERGY==0)
plot(S(i).xd,S(i).yd,'o');
%'o','LineWidth',1, 'MarkerEdgeColor','k', 'MarkerFaceColor','g', 'MarkerSize',8);
end
if (S(i).ENERGY==1)
plot(S(i).xd,S(i).yd,'+');
%,'+','LineWidth',3, 'MarkerEdgeColor','k', 'MarkerFaceColor','r', 'MarkerSize',8);
end
hold on;
end
end
plot(S(n+1).xd,S(n+1).yd,'x');
%
% if((dead1(r))==dead1(r+1))
% d11=0;
% disp(d11);
% else
% disp('1st node dead');
% dead1=[S(i).xd,S(i).yd];
% disp(dead1);
% end
%,'LineWidth',1, 'MarkerEdgeColor','k', 'MarkerFaceColor','r', 'MarkerSize',8);
STATISTICS(r+1).DEAD=dead;
DEAD(r+1)=dead;
%When the first node dies
if (dead==1)
if(flag_first_dead==0)
first_dead=r;
flag_first_dead=1;
end
end
countCHs=0;
cluster=1;
for i=1:1:n
if(S(i).E>0)
temp_rand=rand;
if ( (S(i).G)<=0)
% p1=(p/(1-p*mod(r,round(1/p))));
% p1
%Election of Cluster Heads
if(temp_rand<= (p/(1-p*mod(r,round(1/p)))))
countCHs=countCHs+1;
packets_TO_BS=packets_TO_BS+1;
PACKETS_TO_BS(r+1)=packets_TO_BS;
S(i).type='C';
S(i).G=round(1/p)-1;
C(cluster).xd=S(i).xd;
C(cluster).yd=S(i).yd;
plot(S(i).xd,S(i).yd,'k*');
distance=sqrt( (S(i).xd-(S(n+1).xd) )^2 + (S(i).yd-(S(n+1).yd) )^2 );
% distance
C1(cluster)=distance;
C(cluster).distance=distance;
C(cluster).id=i;
X(cluster)=S(i).xd;
Y(cluster)=S(i).yd;
%Calculation of Energy dissipated
distance;
if (distance>do)
%0000
S(i).E=S(i).E- ( (ETX+EDA)*(4000) + Emp*4000*( distance*distance*distance*distance ));
%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 ));
%S(i).E=S(i).E- ( (ETX+EDA)*(4000) + Efs*4000*( distance * distance ));
end
E(cluster)=S(i).E;
Energy_disp(r+1) = S(i).E;
cluster=cluster+1;
end
end
end
end
STATISTICS(r+1).CLUSTERHEADS=cluster-1;
CLUSTERHS(r+1)=cluster-1;
AllCH = [];
% Try to extracting the CH position and their member node's position
CIa1=0;
clusterData = cell(size(X,2),1);
% cluster_head1=clusterData;
for k=1:size(clusterData)
clusterData{k}.clusterHead = [X(k) Y(k) E(k) C1(k)];
%clusterData{k}.clusterHead
clusterData{k}.clusterNodes = [];
%clusterData{k}.clusterNodes1 = [];
end
for k=1:size(S,2)-1
x = S(k).xd;
y = S(k).yd;
energy = S(k).E;
d_t= distance;
minDist = Inf;
at = 0;
for u=1:size(X,2)
dist = sqrt((x-X(u))^2+(y-Y(u))^2);
if dist<minDist
minDist = dist;
at = u;
% CHbest=max(CH);
% res=0.1;
% CIa1(k)=CIa1(k)+(CH(k)-CHbest(k))*res;
% CH(k)=CH(k)+CIa1(k);
end
end
if at>0
for u=1:size(X,2)
dist1 = sqrt((x-X(u))^2+(y-Y(u))^2);
end
clusterData{at}.clusterNodes = [ clusterData{at}.clusterNodes ; [x y energy dist1] ];
% clusterData{at}.clusterNodes
Next_cluster_head_ND2 = clusterData{at}.clusterNodes(clusterData{at}.clusterNodes(:,3)>=max(clusterData{at}.clusterNodes(:,3))-1E-8,:);
Next_cluster_head=Next_cluster_head_ND2(1,:);
N_C_H=Next_cluster_head;
%member_node=setdiff(clusterData{at}.clusterNodes,clusterData{at},'rows')
% clusterData{at}.clusterNodes1 = [ clusterData{at}.clusterNodes ; [x y energy] ];
% clusterData{at}.clusterNodes1(:,2)=[];
% clusterData{at}.clusterNodes1(:,1)=[];
% %disp('cluster head with max res energy');
% cluster_head_with_max_res_energy =max(clusterData{at}.clusterNodes1)
% clusterData{at}=[x y energy];
% cluster_head=clusterData{at}
cluster_head_member=setdiff(clusterData{at}.clusterNodes,clusterData{at}.clusterHead,'rows');
%disp('cluster head')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%xxxxxxxxxxxxxxxxxxx%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
CH=clusterData{at}.clusterHead;
% CH=cluster_head;
% disp ('cluster head member node')
% cluster_head_member
% disp('Next cluster head')
% N_C_H
% disp('Next cluster head member node')
c = setdiff(cluster_head_member, N_C_H, 'rows');
%clusterData{at}.clusterNodes
%c = clusterData{at}.clusterNodes(~ismember(clusterData{at}.clusterNodes, clusterData{at}))
length_of_member_node= length(c);
ND = length_of_member_node;
AllCH = [AllCH; CH];
AllCH1 = unique(AllCH, 'rows');
%AllCH1
[M,I] = min(AllCH1(:,4));
CHbest = AllCH1(I,:);
% CIa.x(k)=1/(ND);
% CIa.y(k)=1/(ND);
CI(1)=1/(ND);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%position
% CH(k)=0.1;CHbest(k)=0.1;res=0.1;
resmin=0.1;
resmax=1;
res(k)=resmin+(resmax-resmin)*rand;
% CIa.x1(k)=CIa.x1(k)+(AllCH1.x1(k)-CHbest.x1(k))*res;
% CIa.y1(k)=CIa.y1(k)+(AllCH1.y1(k)-CHbest.y1(k))*res;
% CH(k)=CH(k)+CIa(k);
end
end
hold on;
warning('OFF');
[VX,VY]=voronoi(X,Y);
plot(X,Y,'k+',VX,VY,'r-');
axis([0 xm 0 ym]);
AllCH11=[];
AllCH11 = [AllCH11; AllCH1];
disp('All cluster head:');
disp(' ')
disp(' X Y E D');
disp(AllCH11);
l=1;
a11=length(AllCH11);
CIa= zeros(a11,1);
AllCH111(r+1).x1=AllCH1(:,1);
AllCH112(r+1).y1=AllCH1(:,2);
CHbest1(r+1).x1=CHbest(:,1);
CHbest2(r+1).y1=CHbest(:,2);
end
Matlab111
Matlab111 le 4 Déc 2014
thanks you sir,
Roja G
Roja G le 3 Nov 2020
Greetings of the day..!
sir i want to know how to connect basestation and cluster head,i want to know the link between ch and bs code.
sir could you please help me.
Thanx in advance.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Startup and Shutdown dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by