% learncl.m: competitive learning example with clustering % copyright (c) 1996 by Yu Hen Hu % created: 9/3/96 % % mfiles needed: datagen1.m % clear all, clf echo on N=input('# of samples in each cluster = '); N2=N+N; B1=ceil(N/2); B2=N+B1; eta=0.2; means=[.7 -.8 .7 -.8]; var= [0.2 0.2]; x=datagen1([N N],[means;var]); % x is 2N by 2 x=randomize(x); % randomize the ordering of clusters w=rand(2,2)-0.5*ones(2,2); % both neuron initially at random place figure(1),subplot(221),plot(x(:,1),x(:,2),'g.',w(:,1),w(:,2),'r*') title('Initial position') title('initial') for i=1:N2, if norm(x(i,:)-w(1,:)) < norm(x(i,:)-w(2,:)), w(1,:)=w(1,:)+eta*(x(i,:)-w(1,:)); elseif norm(x(i,:)-w(1,:)) > norm(x(i,:)-w(2,:)) w(2,:)=w(2,:)+eta*(x(i,:)-w(2,:)); end if i==B1, figure(1),subplot(222),plot(x(:,1),x(:,2),'g.',w(:,1),w(:,2),'r*') title(['after ' num2str(B1) ' iterations']);drawnow end if i==B2, figure(1),subplot(223),plot(x(:,1),x(:,2),'g.',w(:,1),w(:,2),'r*') title(['after ' num2str(B2) ' iterations']);drawnow end end figure(1),subplot(224),plot(x(:,1),x(:,2),'g.',w(:,1),w(:,2),'r*') title(['at end of ' num2str(N2) ' iterations']) echo off