You are on page 1of 2

EE133A HW1-MATLAB

I = find(labels < 5);


digits = digits(:,I);
labels = labels(:,I);%only condier digits 0-4,reduce # of columns
[n,N]=size(digits);%28*28 image for each col of digits--size
J0=NaN;
z = rand(n,5);%generate initial values
Dis=zeros(5,N);
for iter=1:100
for k=1:5
Dis(k,:)=sqrt(sum((digits-z(:,k*ones(1,N))).^2));
end;
[d,classnew]=min(Dis);
if iter >1
if J1==J0,
break;
end;
J0 = J1;
end;
J1=norm(d)^2*(1/N);
disp(sprintf('J= %e.',J1));
for p = 1:5
I=find(classnew==p);
if size(I,2)
z(:,p)=sum(digits(:,I),2)/size(I,2);
else disp(sprintf('cluster %d is empty.',p));
return;
end
end

end
for k = 1:5
figure()
x=reshape(z(:,k),28,28);
imshow(1-x);
end

The following data was collected from iterations and the final value of J is in
bold, which is the smallest one as we desired.
J= 2.431542e+02.
J= 4.675040e+01.
J= 4.280635e+01.
J= 4.161833e+01.
J= 4.085309e+01.
J= 4.042314e+01.
J= 4.017846e+01.

J= 4.014629e+01.
J= 4.013801e+01.
J= 4.013514e+01.
J= 4.013412e+01.
J= 4.013369e+01.
J= 4.013355e+01.
J= 4.013352e+01.
J= 4.013350e+01.
J= 4.013349e+01.
J= 4.013349e+01.
J= 4.013349e+01.
J= 4.013349e+01.
J= 4.013349e+01.
The following graphs show the five representatives:

You might also like