clear,load field;
figure,imagesc(X),colormap(gray),axis off
figure,hist(X(:),200)
f=[75,100,125,150,175,200,225,250];
sigma=30;beta=1;N=size(X,1);JMAX=length(f);
Y=X;XHAT(N,N)=0;
for J=1:JMAX;E1(:,:,J)=(Y-f(J)).^2;end;
[Z1,K1]=min(E1,[],3);XHAT=f(K1);%min(E1,[],3) is along dim=3.
figure,imagesc(XHAT),colormap(gray),title('MLE'),axis off
for I=1:9;for J=1:JMAX;
E(:,:,J)=([XHAT(2:N,:);XHAT(1,:)]~=f(J))+([XHAT(N,:);XHAT(1:N-1,:)]~=f(J));
E(:,:,J)=E(:,:,J)+([XHAT(:,2:N) XHAT(:,1)]~=f(J))+([XHAT(:,N) XHAT(:,1:N-1)]~=f(J));
E(:,:,J)=2*beta*E(:,:,J)+(Y-f(J)).^2/2/sigma/sigma;end;
[Z,K] = min(E,[],3);XHAT=f(K);end
%subplot(3,3,I),imagesc(XHAT),colormap(gray),axis off,end
figure,imagesc(XHAT),colormap(gray),axis off


