cluster_head.m

来自「无线传感器网络LEACH算法的matlab模型」· M 代码 · 共 26 行

M
26
字号
function [head_coordinate,new_mark]=cluster_head(XY,current_rounds)
global old_mark p left_energy threshold_energy_head
rand('state',sum(100*clock));
[m,n]=size(XY);%计算行数和列数
amount_node=n;%节点数目
threshod_p=p/(1-p*(current_rounds-1));
head=zeros(1,amount_node);
head=rand(1,amount_node);
j=1;%簇头计数器
%head_coordinate=zeros(2,amount_node);
new_mark=zeros(1,amount_node);
amount_head=0;
for i=1:amount_node
    if head(i)<threshod_p&old_mark(i)==0&left_energy(i)>threshold_energy_head
        amount_head=amount_head+1;
    end
end
head_coordinate=zeros(2,amount_head);
for i=1:amount_node
    if head(i)<threshod_p&old_mark(i)==0&left_energy(i)>threshold_energy_head
        head_coordinate(:,j)=XY(:,i);%簇头坐标矩阵
        old_mark(i)=1; %簇头位设为1
        new_mark(i)=1;
        j=j+1; 
    end
end

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?