% FUNCTION Y = MVNPDF(X, [MU], [SIGMA])
%
%   X & MU: vectors of same size
%   sigma: square matrix
%
%   default MU = zeros(size(X))
%           SIGMA = diag(ones(size(X)));

function y = mvnpdf(x,mu,sigma)

if nargin < 2
  mu = zeros(size(x));
end

if nargin < 3
  sigma = diag(ones(size(x)));
end

x = x(:);   % Column vector
mu = mu(:); % Column vector

% Check dimensions
n = length(x);
if size(mu,1) ~= n | size(mu,2) ~= 1 | size(sigma,1) ~= n | ...
      size(sigma,2) ~= n
  error('Parameter dimensions must agree');
end


a = (2*pi)^-(n/2) * det(sigma)^-.5;
b = exp(-.5 * (x-mu).' * inv(sigma) * (x-mu));
y = a * b;
