% hopnet.m - simulates a Hopfield network
%
% The connectivity matrix T must be defined before executing this script
%

[N N]=size(T);

sz=sqrt(N);

if ~exist('net_state','var')
  net_state=2*(rand(N,1)>=0.5)-1;
end

num_iterations=1000;

h=imagesc(reshape(net_state,sz,sz),'EraseMode','none',[-1 1]);
axis square, colorbar
drawnow

for t=1:num_iterations

  % pick a neuron at random to flip
  i=ceil(N*rand);

  if T(i,:)*net_state>0
    net_state(i)=1;
  else
    net_state(i)=-1;
  end

  set(h,'CData',reshape(net_state,sz,sz));
  drawnow

end
