% comphebb.m - competitive Hebbian learning network
%
% assumes data array 'D2' is loaded

X=D2;

[N K]=size(X);   % get array dimensions

num_trials=100000;
eta=0.001;

clf
plot(X(1,:),X(2,:),'.','MarkerSize',1)  % plot data
axis image, axis([-3 3 -3 3])
hold on

M=4;                           % number of neurons
W=randn(N,M);                  % initialize weights
W=W*diag(1./sqrt(sum(W.^2)));  % normalize

% plot weight vectors and get graphics handles for each
for i=1:M
  h(i)=plot([0 W(1,i)],[0 W(2,i)],'k','LineWidth',2,'EraseMode','xor');
end

for t=1:num_trials

  Y=W'*X;  % compute outputs for all data points

  % compute winner for each data point
  
  % implement eq. 9.6/9.7 of HKP
  
  % add something else to avoid local minima
  
  % redraw weight vectors
  for i=1:M
    set(h(i),'XData',[0 W(1,i)],'YData',[0 W(2,i)])
  end

  drawnow
%     pause(0.1)
end

hold off

