function Final = ISODATA(c,Nc,MinMemb,MinSquare,JoinLevel,L,MaxLoops,RawData)
nLOOP = 0;%迭代次数
bSinked = 0;%收敛条件,0不收敛,1收敛
%获得样本维数和样本数目
nSize = size(RawData);
nDim = nSize(1);%向量维数
nTotalData = nSize(2);%样本数目
Final.Result = 0;%返回值:分类结果和各个样本距类心距离
Final.ClassCenter = zeros(nDim,c); %返回值:各类的类心
%建立聚类中心
Z = zeros(nDim,Nc);
MaxN = zeros(1,nTotalData);
%统计各个样本的极值数目
for IndexI = 1:nDim
SubMax = RawData(IndexI,1);
SubN = 1;
for IndexJ = 1:nTotalData
dDot = RawData(IndexI,IndexJ);
if SubMax<=dDot
SubN = IndexJ;
SubMax = dDot;
end
MaxN(SubN) = MaxN(SubN)+1;
end
end
%找到极值最多的样本
SubMax = MaxN(1);
SubMaxN = 1;
for IndexI = 1:nTotalData
if (SubMax<=MaxN(IndexI))
SubMax=MaxN(IndexI);
SubMaxN = IndexI;
end
end
Z(:,1) = RawData(:,SubMaxN);
CenterXY = Z(:,1);
for IndexI = 2:Nc
%找到和当前质心最远的样本作为下一个类IndexI的类心
MaxDis = -1e9;
MaxN = 0;
for IndexJ = 1:nTotalData
dDot = (RawData(:,IndexJ)-CenterXY)'*(RawData(:,IndexJ)-CenterXY);
if MaxDis<=dDot
MaxN = IndexJ;
MaxDis = dDot;
end
end
%记录新的类心
Z(:,IndexI) = RawData(:,MaxN);
%刷新质心
CenterXY = CenterXY + Z(:,IndexI);
CenterXY = CenterXY/2;
end
nZ = zeros(Nc,1);
Result = zeros(2,nTotalData);
%如果两次循环聚类不变化,说明跌代可以结束
OldResult = Result;
%使用迭代次数
while (nLOOP<=MaxLoops & bSinked ~= 1)
nLOOP = nLOOP + 1;
bSepOK = 0;
nZ = zeros(Nc,1);
while (bSepOK==0)
d = zeros(nTotalData,Nc);
for IndexI = 1:nTotalData
dMinN = 0;
dMin = Inf;
for IndexJ = 1:Nc
dDot = (RawData(:,IndexI)-Z(:,IndexJ))'*(RawData(:,IndexI)-Z(:,IndexJ));
d(IndexI,IndexJ) = sqrt(dDot);
if dMin>d(IndexI,IndexJ)
dMin = d(IndexI,IndexJ);
dMinN = IndexJ;
end
end
Result(1,IndexI) = dMinN;
Result(2,IndexI) = dMin;
nZ(dMinN) = nZ(dMinN)+1;
end
OK = 1;
for IndexI = 1:Nc
if (nZ(IndexI)<MinMemb)
TEMP = Z(:,Nc-1);
if IndexI~=Nc-1
TEMP (:,IndexI:Nc-1) = Z(:,IndexI+1:Nc);
end
OK = 0;
Nc = Nc - 1;
nZ = zeros(Nc,1);
break;
end
end
if OK==1
bSepOK = 1;
end
end
%重新计算类的中心
Z = zeros(nDim,Nc);
for IndexI = 1:nTotalData
TEMP = Result(1,IndexI);
Z(:,TEMP) = Z(:,TEMP) + RawData(:,IndexI);
end
for IndexI = 1:Nc
Z(:,IndexI) = Z(:,IndexI)/nZ(IndexI);
end
%计算各类中心到类心的平均距离
dInDis = zeros(1,Nc);
dTDis = 0;
%计算各类的类内平均距离
for IndexI = 1:nTotalData
TEMP = Result(1,IndexI);
dInDis(TEMP) = dInDis(TEMP) + sqrt((RawData(:,IndexI)-Z(:,TEMP))'*(RawData(:,IndexI)-Z(:,TEMP)));
end
for IndexI = 1:Nc
dInDis(IndexI) = dInDis(IndexI)/nZ(IndexI);
dTDis = dTDis + nZ(IndexI)*dInDis(IndexI);
end
%计算总体平均距离
dTDis = dTDis / nTotalData;
%分裂、合并以及 依据nLOOP,Nc判断停止
bNeedDivide = 0;
bNeedJoint = 0;
if (nLOOP == MaxLoops)
JoinLevel = 0;
bNeedJoint = 1;
elseif Nc<=c/2
bNeedDivide = 1;
elseif Nc>=2*c
bNeedJoint = 1;
elseif mod(nLOOP,2)==0
bNeedJoint = 1;
else
bNeedDivide = 1;
end
if (bNeedDivide~=0)
bNeedDivide = 0;
Delta = zeros(Nc,nDim);
for IndexI = 1:nTotalData
TEMP = Result(1,IndexI);
for IndexJ = 1:nDim
Delta(TEMP,IndexJ) = Delta(TEMP,IndexJ) + (RawData(IndexJ,IndexI) - Z(IndexJ,TEMP)).^2;
end
end
for IndexI = 1:Nc
for IndexJ = 1:nDim
Delta(IndexI,IndexJ) = sqrt(Delta(IndexI,IndexJ)./nZ(IndexI));
end
end
DeltaMax = max(Delta');
DeltaMaxN = DeltaMax;
for IndexI = 1:Nc
for IndexJ = 1:nDim
if (Delta(IndexI,IndexJ) ==DeltaMax(IndexI))
DeltaMaxN(IndexI) = IndexJ;
break;
end
end
end
%判断是否需要分裂
IndexI=0;
for IndexI = 1:Nc
if DeltaMax(IndexI)>MinSquare
if (dInDis(1,IndexI)>dTDis | (Nc ==1)) & (nZ(IndexI)>2*(MinMemb+1)) & (Nc<=c/2)
bNeedDivide = 1;
break;
end
end
end
if (bNeedDivide == 0)
bNeedJoint = 1;
else
%进行分裂操作
TEMP = zeros(nDim,Nc+1);
TEMP(:,1:Nc) = Z;
TEMP(:,Nc+1) = Z(:,IndexI);
DT = Delta';
TEMP(DeltaMaxN(IndexI),IndexI) = TEMP(DeltaMaxN(IndexI),IndexI) + 0.5*DT(DeltaMaxN(IndexI),IndexI);
TEMP(DeltaMaxN(IndexI),Nc+1) = TEMP(DeltaMaxN(IndexI),Nc+1) - 0.5*DT(DeltaMaxN(IndexI),IndexI);
Nc = Nc + 1;
Z = TEMP;
end
end
%合并处理
if (bNeedJoint~=0)
%计算各个类对中心距离
ED = zeros(Nc,Nc);
for IndexI = 1:Nc
for IndexJ = 1:Nc
if (IndexI<IndexJ)
ED(IndexI,IndexJ) = sqrt((Z(:,IndexI)-Z(:,IndexJ))'*(Z(:,IndexI)-Z(:,IndexJ)));
else
ED(IndexI,IndexJ) = Inf;
end
end
end
Sorted = zeros(L,2);
for IndexK = 1:L
%找到最近的类
MinDis = Inf;
MinPos = [0,0];
for IndexI = 1:Nc
for IndexJ = 1:Nc
if (IndexI<IndexJ)
if MinDis>=ED(IndexI,IndexJ)
MinDis = ED(IndexI,IndexJ);
MinPos = [IndexI,IndexJ];
end
end
end
end
%判断最近的距离是否小于JoinLevel
if MinDis>=JoinLevel
break;
end
Sorted(IndexK,1) = MinPos(1);
Sorted(IndexK,2) = MinPos(2);
ED(MinPos(1),:) = Inf;
ED(:,MinPos(2)) = Inf;
end
%合并类
count = 0;
for IndexI = 1:L
if (Sorted(IndexI,1)==0|Sorted(IndexI,2)==0)
break;
end
TEMP =
评论0