Sie sind auf Seite 1von 2

1/6/15 8:40 PM

D:\!!!!!Course_Fall_2014\ai_lab_super_final\matlab_17_dec_2014\MA...\k_means2.m

1 of 2

function [data group c]= k_means2(k)


%point 1 : (1,1)(x,y), point 2 : (1,2)(x,y)...
x=[1 1 1.5 2 2 1 2 2.5 3 4 5 5 4.5 5.5 5 6 6];
y=[1 2 1.5 1 2 3 3 2.5 2 4 3 4 3.5 4.5 5 4 5];
data = [x ; y];
%{
data=
1 1 1.5 2....
1 2 1.5 1..
%}
[r numData] = size(data);
%r
= number of row in data matrix (2)
%numData = Number of column in data matrix (17)
center = data(:, 1:k);
%assume k = 2, then:
%copies all rows of "data" and 1 to 2 column of "data" in "center".
%here we are choosing 2 points from "data". these 2 points are initial
%centers of 2 clusters(groups). any 2 points of any position can be
%takem as initial centers but for faster processing we have taken 2
%points from "data" as initial centers.
%{
center=
1 1
1 2
%}
plot(x,y,'*','MarkerSize',10);
%ploting "*" in x,y axis point (plot is taking x axis's & y axis's
%info from "x" & "y" respectively),"MarkerSize" is used to specify the size of the star
axis([0 6.5 0 5.5])
hold on
plot(center(1,:),center(2,:),'rs','MarkerSize',10);
%plotting centers by taking the x & y axis's information from
%"center(1,:)& center(2,:)" respectively. "rs" parameter is used to plot
%rectangles, & "MarkerSize" parameter is used to specify size of the
%rectangle.
hold off

[cr cc] = size(center);


%cr=rowsize of center
%cc= column size of center
oldCenter = zeros(cr,cc);
%zeros(cr,cc) creates a matrix named "oldCenter" with rowsize=cr,
%columnsize=cc and all elements in the matrix is assigned with 0 (zero).
c = ['b' 'r' 'g' 'c' 'm' 'y' 'k']; %Parameters of plot functioin for coloring cluster, blue,red,
green,cyan magenta, yellow, black respectively given.

while(~isequal(center,oldCenter)) %as long as previous center and the new center is not equal the
loop will continue.
D = zeros(k,numData);
G = zeros(k,numData);

%k is number of groups, numData is number of points.

for j = 1:k %k is number of clusters


for i = 1: numData %numData is number of points given in "data"

1/6/15 8:40 PM

D:\!!!!!Course_Fall_2014\ai_lab_super_final\matlab_17_dec_2014\MA...\k_means2.m

2 of 2

D(j,i) = find_distance(data(:,i),center(:,j)); %returning every point's distance from


every center and storing in "D", "D"'s first row contains first center's distance from every point of
"data" and "D"'s second row contains second center's distance from every point of "data" and so on...
end
end
for i=1:numData
id = find(D(:,i) == min(D(:,i))); %returning every columun's minimum number's row positions
from "D"
G(id(1),i) = 1; %id(1) means take the first value (because sometime "id" contains multiple
values)
end
oldCenter = center;
for i=1:k
group{i} = find(G(i,:) == 1); %returning positions where 1 is found on "G" (row wise).
tmpData = data(:,group{i}); %returning all rows of "data" with columns that matches with
group(i) list.
center(1,i) = sum(tmpData(1,:))/length(tmpData(1,:)); %mean of x axis points and storing in
center's x axis as new x axis of center.
center(2,i) = sum(tmpData(2,:))/length(tmpData(2,:)); %mean of y axis points and storing in
center's y axis as new y axis of center.
end

end

plot(x,y,'*','MarkerSize',10); %to hide the previous plots. (as "hold on" will be executed so this
line of code must be executed otherwise previous plot's initial rectangled centers will be visible in
the final plot(that is not what we want).)
hold on %holding the previous plot (superimposition mode on). Meaning all the next plots will be
superimposed with the previous plot until "hold off" is executed.
for i= 1:k
plot(data(1,group{i}),data(2,group{i}),'*','Color',c(i),'MarkerSize',10); %plotting each group
with diffent color
plot(center(1,i),center(2,i),'ks','LineWidth',2,'MarkerFaceColor',c(i),'MarkerSize',10); %
plotting center of each group with different colored filled rectangle
end
hold off %superimposition mode off
axis([0 6.5 0 5.5]) %x axis's screen size is set: from 0 to 6.5 and Y axis's screen size is set:
from 0 to 5.5

end

function d = find_distance(d,c)

d = sqrt( (d(1)-c(1))^2 + (d(2)-c(2))^2 ); %calculating distance using pythagorean theorem

end

Das könnte Ihnen auch gefallen