% Median filter

function y = medfilt2(x)

index = [-1 0 1];
index = [index-size(x,1) index index+size(x,1)] + 2*size(x,1);
y = zeros(size(x));
x1 = [zeros(size(x,1),2) x zeros(size(x,1),2)];
for n = 1:prod(size(x));
    y(n) = median(x1(index + n));    
end

