%Sample Ising model  
%Lattice
M = 128; N = 128;
nbrs=GetNbrs(M,N);

%smoothing parameter
theta = 0.42;

%initialise MCMC X_0=x to agree with g where data is present
x = round(rand(M,N));
figure(3);hf=imagesc(x);colormap(gray);axis square;drawnow;
s=0;
SS=10000;

while 1,
   pixel = ceil(rand*M*N); 
   hashfn=(x(nbrs{pixel})~=x(pixel));
   disagree = sum(hashfn);
   agree = sum(~hashfn);
   if log(rand) < 2*theta*(disagree-agree)
      x(pixel) = 1-x(pixel); 
   end
   s = s + 1;
   if rem(s,SS)==0,
      set(hf,'CData',255*x); drawnow;
   end
end


