function b = extend_array(a, padSize, padVal)

numDims = numel(padSize);
idx   = cell(1,numDims);
sizeB = zeros(1,numDims);
for k = 1:numDims
    M = size(a,k);    
    idx{k}   = 1:M;
    sizeB(k) = M + padSize(k);
end

% Initialize output array with the padding value.  Make sure the
% output array is the same type as the input.
b         = repmat(padVal, sizeB);
b(idx{:}) = a;


end