⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 kmean.m

📁 K-mean 模式識別分析法使用k-mean作為分析方法
💻 M
字号:
function [Z, Xcluster, Ycluster, cluster] = kmean(X,Y,k)

%%%%%%%%%%%%%%%%
%Parameters
%%%%%%%%%%%%%%%%
s=size(X);
s=s(2);
cluster=zeros(1,s); % Temporary space inside the function.
changes=1;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Step1.Initialization of the centers
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Z= initialize_centers(X, Y, k);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Loop iterative clustering (Main Program)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
while changes==1,
    changes=0;
    
    % Step2. Calculates and assigns to each coordinate (X, Y) the closest.
    for i=1:s,
        m=close(X(i), Y(i), Z, k);
        if  m~=cluster(i),  % Compare the previous value of the nearest center.
            changes=1;
        end;
        cluster(i)=m;
    end;
    
    % Step3. If you have changed any allowance recalculate the centers
    %Compute new cluster centers for each set Si(n)
    if changes==1,
        Z=recalculate(cluster, X, Y, k,Z);
    end;
end;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% K separate points in the vectors representing patterns k%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Xcluster=0;
Ycluster=0;
for m=1:k
    inedx=0;
    index=find(cluster==m);
    s2=size(index);
    s2=s2(2);
    for n=1:s2
        Xcluster(1,n,m)= X(index(n));
        Ycluster(1,n,m)= Y(index(n));
    end;
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                     %End of main program
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %              	Code of subfeatures used                %%  
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  Function to calculate the closest point to each center%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [m] = close(x, y, Z, k)
    dtemp=0;
    d=0;
    for j=1:k
        P=[x y];
        d=distance(Z(j,:), P); % Distance from the center to points
        if j<2,
            m=j;    % The first distance is always valid.
            dtemp=d;
        elseif d < dtemp,
            m=j;    % We stayed with the center corresponding to the smallest of distances.
            dtemp=d;  
        end;
    end;
    
    
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Code of subfeatures used
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [Zout1] = recalculate(cluster, X, Y, k, Z) %Take the average of the points assigned to each class corersp. to centers                                                                     
	s=size(X);
	s=s(2);
	valor=zeros(1,k);
	Zout=zeros(k,2);
	for m=1:k
        index=find(cluster==m);
        if isempty(index)==0
            sindex=size(index);
            sindex=sindex(2);  
            Zout1(m,1)=(sum(X(index))) / sindex;
            Zout1(m,2)=(sum(Y(index))) / sindex;        
        else
            Zout1(m,:)=Z(m,:);
        end;
	end;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 % Calculates the distance between two %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [dist]=distance(P1, P2)
    dist=sqrt( ((P1(1)-P2(1))^2) + ((P1(2)-P2(2))^2) );% Distance between two points.
    
    
%%%%%%%%%%%%%%%%%%%%%%%%%%%%	
% Initialize the centers
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [Z]=initialize_centers(X, Y, k)
    % Distributed evenly centers
    dx= (max(X))-(min(X));
    dy= (max(Y))-(min(Y));
    dzx= dx/(k+1);    % Distance between centers coordinates X.-->radius
    dzy= dy/(k+1);    % Distance between centers coordinates Y.-->radius
    for i=1:k,
        Z(i,1)=min(X)+(dzx*i);
        Z(i,2)=min(Y)+(dzy*i);
    end;

⌨️ 快捷键说明

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