function minVal = minInNeighborhoodV3(M, loc)
% minVal is the lowest value in the neighborhood of position 
% (loc(1),loc(2)), i.e., M(loc(1)-1:loc(1)+1, loc(2)-1:loc(2)+1).
% M is a matrix of numbers.  loc is a vector of length 2.

[nr, nc]= size(M);  % dimensions of M

% Build a border around M using NON-VECTORIZED code
bigM= zeros(nr+2,nc+2);
for r = 1:nr+2
    for c = 1:nc+2;
        if r==1 || r==nr+2 || c==1 ||c==nc+2
            bigM(r,c)= realmax;
        else
            bigM(r,c)= M(r-1,c-1);
        end
    end
end

nr= nr+2;  nc= nc+2;        % dimensions of bigM
r= loc(1)+1;  c= loc(2)+1;  % center of neighborhood in bigM

minVal= minInMatrix( bigM(r-1:r+1, c-1:c+1) );
