function minVal = minInNeighborhood(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
bigM= [ ones(nr,1)*realmax  M  ones(nr,1)*realmax ];
bigM= [ ones(1,nc+2)*realmax;  bigM;  ones(1,nc+2)*realmax ];

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) );