function minVal = minInNeighborhoodV2(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 by assigning M into bigM .
% This is vectorized code.
bigM= ones(nr+2,nc+2)*inf;
bigM(2:nr+1,2:nc+1)= M;

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) );
