clear all
clc

load('I4.txt');
x(:,:,1)=I4;
[n1,n1]=size(I4);
loop=5;


figure(1)
imagesc(x(:,:,1));
title('Initial Image');
saveas(figure(1),['Initial Image prob4-4.png']);


for i=2:loop;
    x(:,:,i)= x(:,:,i-1);
    for j=1:n1;
        for k=1:n1;
            [n2,n2,n3]=size(x);
            if (j==1) && (k==1)
               mid=(x(j,k+1,i-1)+x(j+1,k,i-1))/2;
            end;

            if (j==1) && (k==n2)
               mid=(x(1,k-1,i-1)+x(j+1,k,i-1))/2;
            end;
            
            if (j==n2) && (k==1)
               mid=(x(j-1,k,i-1)+x(j,k+1,i-1))/2;
            end;
            
            if (j==n2) && (k==n2)
               mid=(x(j,k-1,i-1)+x(j-1,k,i-1))/2;
            end;
            
            if (j==1 && k~=1 && k~=n2)
               mid=(x(j+1,k,i-1)+x(j,k-1,i-1)+x(j,k+1,i-1))/3;
            end;
            
            if (j==n1 && k~=1 && k~=n2)
               mid=(x(j-1,k,i-1)+x(j,k-1,i-1)+x(j,k+1,i-1))/3;
            end;
            
            if (j~=1 && j~=n2 && k==1)
               mid=(x(j-1,k,i-1)+x(j+1,k,i-1)+x(j,k+1,i-1))/3;
            end;
            
            if (j~=1 && j~=n2 && k==n2)
               mid=(x(j-1,k,i-1)+x(j+1,k,i-1)+x(j,k-1,i-1))/3;
            end;            
            
            if (j~=1 && j~=n2) && (k~=1 && k~=n2)
               mid=(x(j-1,k,i-1)+x(j+1,k,i-1)+x(j,k+1,i-1)+x(j,k+1,i-1))/4;
            end;
            
            norm1=random('normal',mid,sqrt(0.1),1,1);
            x(j,k,i)=norm1;  
        end;
    end;
figure(i)
imagesc(x(:,:,i-1));
title(['Picture ' int2str(i)]);
saveas(figure(i),['Picture ' int2str(i) ' prob4-4.png']);
end;
