function B = movingAvg(A)
%MOVINGAVG Moving average on a matrix
% B = movingAvg(A) computes the 3x3 moving average on
% a matrix A.
%
% B(row, col) is zero where a 3x3 neighbourhood does not
% exist at A(row, col).

[rows, cols] = size(A);
B = zeros(rows, cols);
for r = 2:rows - 1
    for c = 2:cols - 1
        neighbourhood = A(r - 1: r + 1, c - 1: c + 1);
        B(r, c) = mean(neighbourhood(:));
    end
end
