OK, a few more things. I think you're referring to Hadhoud and Thomas' paper in the May 1988 issue of IEEE Transactions on Circuits and Systems, Vol 35. No. 5. I see at least a couple more errors but I only read the paper over a few minutes.
D(2:M,2:N)=X(2:M,2:N); % D is shifted version of X
You're not actually doing a shift here, in the paper there's a shift by 1 pixel in the X and Y directions.
D(2:M,2:N)=X(1:M-1,1:N-1); % D is a shiftier version of X
They also initialize the weights, W, by running the algorithm over 10 rows of the original image starting with a 0 weight matrix.
I also believe (not 100% certain yet) that your weight udpate is incorrect. Your weights are continuously updating which introduces some strange smearing into the algorithm. From my brief read of the paper the weights are only updated when your n loop iterates. The weights are also scaled so that their total sum is 1, which preserves the local mean value.