% Calculate a two-dimensional Normal pdf
%    x can be a matrix of column vectors.

function z = normal2d_pdf (x, m, S)

   % Get x and m in the right form

   if ((columns (x) > rows (x)) && (rows (x) == 1))
         x = x';
   end
   if (columns (m) > rows (m))
      m = m';
   end

   % Calculate the constant
   iS = inv(S);
   c = 1/(2 * pi * sqrt(det (S)));

   % Do the calculations for each x
   for ct=1:columns (x)
      y = x (:,ct);
      md = (y.-m)' * iS * (y.-m);
      z (ct) = c * exp (-0.5 * md);
   end


endfunction
