function [rows, cols] = hood(r, c, nrows, ncols, cross)
% HOOD Compute the indices of cells in the neighborhood with wrap-around boundary conditions
% [rows, cols] = hood(r, c, nrows, ncols, cross)
%
% If cross = 0, we use a 3x3 neighborhood, scanning down each column in turn
% If cross = 1, we return the indices of cells in the middle, to the north, east, south, and west

if ~cross
  u = wrap(c-1:c+1, ncols);
  C = repmat(u(:)', [3 1]);
  u = wrap(r-1:r+1, nrows);
  R = repmat(u(:), [1 3]);
  
  rows = R(:)';
  cols = C(:)';
else
  if r > 1 & r < nrows
    rows = [r r-1 r r+1 r];
  else
    rows = [r wrap(r-1,nrows) r wrap(r+1,nrows) r];
  end
  if c > 1 & c < ncols
    cols = [c c c+1 c c-1];
  else
    cols = [c c wrap(c+1,ncols) c wrap(c-1,ncols)];
  end
end
