% 2-d convolution 3x3 averaging filter, row-column method 
clear
x=[1 -1 3 2;
    2 1 2 4;
    1 -1 2 -2;
    3 1 2 2]; % input 4x4 image
% choose one of the border extensions, lines 8,17 or 9,18
xe = padarray(x,[1 1],'replicate') % replicated
%xe = padarray(x,[1 1]); % zero-padded
[R C]=size(x); % size of the input image
for r=1:R
  for c=1:C % row filter
    x(r,c)=xe(r+1,c)+xe(r+1,c+1)+xe(r+1,c+2);
  end
end
x
xe = padarray(x,[1 1],'replicate'); % replicated
%xe = padarray(x,[1 1]); % zero-padded
for c=1:C
  for r=1:R % coloum filter
    x(r,c)=xe(r,c+1)+xe(r+1,c+1)+xe(r+2,c+1);
  end
end
display('scaled output')
x % output

