% 2-d median filter
clear
x=[23,51,23,32;
    32,44,44,23;
    23,23,44,32;
    44,44,23,23]; % input
xe = padarray(x,[1 1],'replicate') % replicated
[R C]=size(x)
for r=1:R
  for c=1:C 
    m = reshape(xe(r:r+2,c:c+2),1,9);
    m=sort(m);xout(r,c)=m(5);
  end
end
xout


