📄 leader_follower_matrix.m
字号:
function [xy_matrix,w_class,w_class_num,class_number]=leader_follower_matrix(xy_matrix,i,w_class,w_class_num,constant)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%leader_follower algorithm: (process one sample each iteration)
% to get the class whitch has nearest distance
% (smaller than the threhold) to the new sample,
% and conflate the new sample to the very class.
% if the minimum distance is higher than the
% threhold,the make the new sample a independent
% class.
% input value:
% sub_matrix: each row is a sample
% w_class: contain the cluster centers which have been found
% w_class_num:contain the number of each class which has been found
% constant: the threhold value defined before the program
% output value:
% sub_matrix: include the message of classID
% w_class: the updated cluster centers
% w_class_num: the updated numbers of each class
% class_number: current number of whole classes
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
old_class_size=size(w_class,1);
sample_size=size(xy_matrix,1);
for class_id=1:old_class_size
ww(1,class_id)=sqrt(double((w_class(class_id,1)-xy_matrix(i,1))^2+(w_class(class_id,2)-xy_matrix(i,2))^2));
end
new=min(ww);%当前样本聚类各个聚类中心的最小值
if (new<=constant)
num_id=0;
for class_id=1:old_class_size
if (new==ww(1,class_id)) % 找到距离样本点最近的聚类中心class_id及其个数num_id+1
num(1,(num_id+1))=class_id;%将与样本距离相等的聚类中心的id保存在num中(1*n)
num_id=num_id+1; %num_id即将合并的聚类中心数目
end
end
sum_value=zeros(1,2);
sum_p=0;
% if (new<=constant)
for id=1:num_id %num()是变化前的类别标识符
temp_1_matrix=num(1,id)*ones(sample_size,1);
L1=(xy_matrix(:,3)==temp_1_matrix);
xy_matrix(:,3)=xy_matrix(:,3)+(-(num(1,id)+1))*L1;
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%$$$$$$ end %%%将即将被合并的样本之类别标记改为-1
iden=max(xy_matrix(:,3));
if num(1,id)<iden%%%%将要合并得标志符不是最大标志符时:
change=num(1,id)+1;
temp_2_matrix=change*ones(sample_size,1); % matrix calculalte
L2=(xy_matrix(:,3)>=temp_2_matrix);
xy_matrix(:,3)=xy_matrix(:,3)+(-1)*L2;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end %%%将其他类的标识符减少1
p=w_class_num(num(1,id),1); %得到即将合并各类的已归类样本数目
sum_value=sum_value+p*w_class(num(1,id),:); %计算即将合并各类的所有样本之和
sum_p=sum_p+p; %合并的各类中样本数目之和
w_class_num(num(1,id),:)=[]; %将被合并的样本数目去掉
w_class(num(1,id),:)=[]; %将被合并的样本的聚类中心去掉
end
w_new=double((sum_value+xy_matrix(i,1:2))/(sum_p+1)); %计算出新类别的聚类中心
% w_class=[w_class;w_new]; %将新聚类中心加到聚类中心矩阵末尾
w_class=vertcat(w_class,w_new);
w_class_num=[w_class_num;(sum_p+1)];%将新类别样本数加到各个类别样本数矩阵末尾
% w_class_element
else %如果距离当前所有聚类中心距离都大于阈值,则自立一类
% w_class=[w_class;sample];
w_class=vertcat(w_class,xy_matrix(i,1:2));
w_class_num=[w_class_num;1];
end
class_number=size(w_class,1);%得到所有类别标识总数&&也是当前得到的新类别的标识
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -