estimateGaussian.m 993 B

12345678910111213141516171819202122232425262728293031323334353637
  1. function [mu sigma2] = estimateGaussian(X)
  2. %ESTIMATEGAUSSIAN This function estimates the parameters of a
  3. %Gaussian distribution using the data in X
  4. % [mu sigma2] = estimateGaussian(X),
  5. % The input X is the dataset with each n-dimensional data point in one row
  6. % The output is an n-dimensional vector mu, the mean of the data set
  7. % and the variances sigma^2, an n x 1 vector
  8. %
  9. % Useful variables
  10. [m, n] = size(X);
  11. % You should return these values correctly
  12. mu = zeros(n, 1);
  13. sigma2 = zeros(n, 1);
  14. % ====================== YOUR CODE HERE ======================
  15. % Instructions: Compute the mean of the data and the variances
  16. % In particular, mu(i) should contain the mean of
  17. % the data for the i-th feature and sigma2(i)
  18. % should contain variance of the i-th feature.
  19. %
  20. mu = 1/m * sum(X);
  21. sigma2 = 1/m * sum((X .- mu) .^2);
  22. % =============================================================
  23. end