20 February 2014

Clustering in Matlab


Sample program for Clustering in matlab:     km_demo.m
% Demo for the kmeans algorithm

% First, generate sample data
%   We will test with 4 clusters in 3 dimensions,
%   by generating random data with gaussian density, variance 1,
%   with means (0,0,0), (0,0,6), (0,6,0) and (6,0,0)
%   and Ndata  200,       300,   100     and 500

K = 0;
dim = 3;
variance = 1;
sdev = sqrt(variance);
prompt={'Enter Number of Cluster(value between 2 to 4):'};
name='Input for Number of cluster';
numlines=1;
defaultanswer={'2'};
answer=inputdlg(prompt,name,numlines,defaultanswer);
x1=cell2mat(answer);
K=str2num(x1);
while(K>=7)
  answer=inputdlg('Wrong Input. Enter Number of Cluster(value between 2 to 6): ',name,numlines,defaultanswer);
x1=cell2mat(answer);
K=str2num(x1)
end
if(K==2)
 cluster1 = sdev*randn(200,dim) + kron(ones(200,1),[0,0,0]);
cluster2 = sdev*randn(300,dim) + kron(ones(300,1),[0,0,6]);
X = [cluster1 ; cluster2];
end
    if(K==3)
       cluster1 = sdev*randn(200,dim) + kron(ones(200,1),[0,0,0]);
cluster2 = sdev*randn(300,dim) + kron(ones(300,1),[0,0,6]);
cluster3 = sdev*randn(100,dim) + kron(ones(100,1),[0,6,0]);
X = [cluster1 ; cluster2 ; cluster3];
    end
   
 if(K==4)
    cluster1 = sdev*randn(200,dim) + kron(ones(200,1),[0,0,0]);
cluster2 = sdev*randn(300,dim) + kron(ones(300,1),[0,0,6]);
cluster3 = sdev*randn(100,dim) + kron(ones(100,1),[0,6,0]);
cluster4 = sdev*randn(500,dim) + kron(ones(500,1),[6,0,0]);
X = [cluster1 ; cluster2 ; cluster3; cluster4];
end

% Build data matrix


% Now apply K-means algorithm
% Note that order of results may vary
maxerr = 0;
[proto Nproto] = simple_kmeans(X,K,maxerr);
msgbox('Press OK to watch Cluster 1','Cluster1 Diagram');
pause(6);
plot(cluster1,'DisplayName','cluster1','YDataSource','cluster1');figure(gcf,'cluster1');
pause(20);
msgbox('Press OK to watch Cluster 2','Cluster2 Diagram');
pause(6);
plot(cluster2,'DisplayName','cluster2','YDataSource','cluster2');figure(gcf,'cluster2');
pause(20);
msgbox('Press OK to watch Cluster 3','Cluster3 Diagram');
pause(6);
plot(cluster3,'DisplayName','cluster3','YDataSource','cluster3');figure(gcf,'cluster3');
pause(20);
msgbox('Press OK to watch Cluster 4','Cluster4 Diagram');
pause(6);
plot(cluster4,'DisplayName','cluster4','YDataSource','cluster4');figure(gcf,'cluster4');

Sample code : simple_kmeans.m

function [means,Nmeans] = simple_kmeans(X,K,maxerr)


[Ndata, dims] = size(X);
dist = zeros(1,K);

for i=1:K-1
   means(i,:) = X(i,:);
end
means(K,:) = mean(X(K:Ndata,:));

cmp = 1 + maxerr;
while (cmp > maxerr)
   % Sums (class) and data counters (Nclass) initialization
   class = zeros(K,dims);
   Nclass = zeros(K,1);

   % Groups each elements to the nearest prototype
   for i=1:Ndata
      for j=1:K
         % Euclidean distance from data to each prototype
         dist(j) = norm(X(i,:)-means(j,:))^2;
      end
      % Find indices of minimum distance
      index_min = find(~(dist-min(dist)));
      % If there are multiple min distances, decide randomly
      index_min = index_min(ceil(length(index_min)*rand));
      class(index_min,:) = class(index_min,:) + X(i,:);
      Nclass(index_min) = Nclass(index_min) + 1;
   end
   for i=1:K
      class(i,:) = class(i,:) / Nclass(i);
   end

   % Compare results with previous iteration
   cmp = 0;
   for i=1:K
      cmp = norm(class(i,:)-means(i,:));
   end

   % Prototype update
   means = class;
end


Nmeans = Nclass;


This program gives you a better understanding of image clustering.

0 comments:

Post a Comment

Thank you for comment. We will try to enhance the quality of this website.