multivariateGaussian.m 829 B

12345678910111213141516171819202122
  1. function p = multivariateGaussian(X, mu, Sigma2)
  2. %MULTIVARIATEGAUSSIAN Computes the probability density function of the
  3. %multivariate gaussian distribution.
  4. % p = MULTIVARIATEGAUSSIAN(X, mu, Sigma2) Computes the probability
  5. % density function of the examples X under the multivariate gaussian
  6. % distribution with parameters mu and Sigma2. If Sigma2 is a matrix, it is
  7. % treated as the covariance matrix. If Sigma2 is a vector, it is treated
  8. % as the \sigma^2 values of the variances in each dimension (a diagonal
  9. % covariance matrix)
  10. %
  11. k = length(mu);
  12. if (size(Sigma2, 2) == 1) || (size(Sigma2, 1) == 1)
  13. Sigma2 = diag(Sigma2);
  14. end
  15. X = bsxfun(@minus, X, mu(:)');
  16. p = (2 * pi) ^ (- k / 2) * det(Sigma2) ^ (-0.5) * ...
  17. exp(-0.5 * sum(bsxfun(@times, X * pinv(Sigma2), X), 2));
  18. end