0% found this document useful (0 votes)
27 views3 pages

All All: %jumlah Data Pada Cluster

The document generates random data points and initializes cluster centroids. It then iteratively assigns data points to the closest centroid and recalculates the centroids based on the mean of each cluster. This clustering algorithm runs until the centroids stabilize. In the end, it displays the final centroids, number of iterations, and cluster sizes.

Uploaded by

Noveta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views3 pages

All All: %jumlah Data Pada Cluster

The document generates random data points and initializes cluster centroids. It then iteratively assigns data points to the closest centroid and recalculates the centroids based on the mean of each cluster. This clustering algorithm runs until the centroids stabilize. In the end, it displays the final centroids, number of iterations, and cluster sizes.

Uploaded by

Noveta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

clc;

clear all;
close all;

randnumb = 100;
M = zeros(randnumb,2);
label = zeros(randnumb,1);

for a=1:randnumb
M(a,1) = unidrnd(randnumb);
M(a,2) = unidrnd(randnumb);
end

c1 = [unidrnd(randnumb) unidrnd(randnumb)];
c2 = [unidrnd(randnumb) unidrnd(randnumb)];
c1_temp = -ones(1,2);
c2_temp = -ones(1,2);

iterasi = 0;
while((c1(1,1)~=c1_temp(1,1)) && (c1(1,2)~=c1_temp(1,2)) && (c2(1,1)~=c2_temp(1,1))
&& (c2(1,2)~=c2_temp(1,2)))
iterasi = iterasi+1;
jum_c1 = 0;
jum_c2 = 0;
for a=1:randnumb
x = M(a,1);
y = M(a,2);
c1x = c1(1,1);
c1y = c1(1,2);
c2x = c2(1,1);
c2y = c2(1,2);

euc1 = sqrt((x - c1x)^2 + (y - c1y)^2);


euc2 = sqrt((x - c2x)^2 + (y - c2y)^2);

if(euc1 <= euc2)


label(a,1)=1;
jum_c1 = jum_c1+1; %jumlah data pada cluster
plot(M(a,1),M(a,2),'ob'); hold on
else
label(a,1)=2;
jum_c2 = jum_c2+1;
plot(M(a,1),M(a,2),'or'); hold on
end
end
jx_c1 = 0;
jy_c1 = 0;
jx_c2 = 0;
jy_c2 = 0;
for a=1:randnumb
if(label(a,1)==1)
jx_c1 = jx_c1 + M(a,1);
jy_c1 = jy_c1 + M(a,2);
else
jx_c2 = jx_c2 + M(a,1);
jy_c2 = jy_c2 + M(a,2);
end
end

rx_c1 = jx_c1/jum_c1;
ry_c1 = jy_c1/jum_c1;
rx_c2 = jx_c2/jum_c2;
ry_c2 = jy_c2/jum_c2;

c1_temp(1,1) = c1(1,1);
c1_temp(1,2) = c1(1,2);
c2_temp(1,1) = c2(1,1);
c2_temp(1,2) = c2(1,2);

c1(1,1) = round(rx_c1);
c1(1,2) = round(ry_c1);
c2(1,1) = round(rx_c2);
c2(1,2) = round(ry_c2);

disp('centroid baru 1 & 2');


disp(c1);
disp(c2);

disp('Iterasi ke');
disp(iterasi);
end
disp('Jumlah data biru');
disp(jum_c1);
disp('Jumlah data merah');
disp(jum_c2);
plot(c1(:,1),c1(:,2),'xb','MarkerSize',15,'LineWidth',3); hold on
plot(c2(:,1),c2(:,2),'xr','Markersize',15,'LineWidth',3); hold on
grid on

You might also like