%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%% Kohonen's Self Orginizing Map (SOM)
%%%%%%%%%%%%%%%% One dimension
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear;
close all hidden;
clf;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
n=100;
K=20;
TTT=500;
epsilon1=0.002;
DATACASE=1; %%%%% 1 2 3
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
x=zeros(n,2);
y=zeros(K,2);
z=zeros(K,2);
%%%%%%%%%%%%%%%%% Training Data %%%%%%%%%%%%%%%
if(DATACASE==1)
x(:,1)=4*rand(n,1)-2;
x(:,2)=x(:,1).^2+0.5*randn(n,1)+2;
elseif(DATACASE==2)
RRR=rand(n,1);
x(:,1)=3*cos(2*pi*RRR)+0.2*randn(n,1);
x(:,2)=3*sin(2*pi*RRR)+0.2*randn(n,1);
elseif(DATACASE==3)
for i=1:1:(n/2)
x(i,1)=4*( -(n/4)+i )/(n/2) +0.1*randn(1,1);
x(i,2)=0.1*randn(1,1);
end
for i=1:1:(n/2)
x(i+n/2,1)=0.1*randn(1,1);
x(i+n/2,2)=4*( -(n/4)+i )/(n/2) + 0.1*randn(1,1);
end
end
%%%%%%%%%%%%%%%%%%%% Initialization %%%%%%%%%%%
for k=1:1:K
y(k,1)=(-k+K/2)/K;
y(k,2)=0.5;
end
%%%%%%%%%%%%%%%%%%%% Distance %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
dist=@(i,x,y)((x(i,1)-y(:,1)).^2+(x(i,2)-y(:,2)).^2);
%%%%%%%%%%%%%%%%%%%%% Figure 1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure(1);
hold on;
plot(x(:,1),x(:,2),'b.');
plot(y(:,1),y(:,2),'ko-');
title('SOM: Training Process');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for kur=1:1:TTT
%%%%%%%% SOM Learning
for i=1:1:n
dd=dist(i,x,y);
[minval,mink]=min(dd);
y(mink,:)=y(mink,:)+2*epsilon1*(x(i,:)-y(mink,:)) ;
if(mink>1)
y(mink-1,:)=y(mink-1,:)+epsilon1*(x(i,:)-y(mink-1,:));
end
if(mink>2)
y(mink-2,:)=y(mink-2,:)+epsilon1*(x(i,:)-y(mink-2,:));
end
if(mink