|
|
@ -1,11 +1,11 @@
|
|
|
|
function [mu sigma2] = estimateGaussian(X)
|
|
|
|
function [mu sigma2] = estimateGaussian(X)
|
|
|
|
%ESTIMATEGAUSSIAN This function estimates the parameters of a
|
|
|
|
%ESTIMATEGAUSSIAN This function estimates the parameters of a
|
|
|
|
%Gaussian distribution using the data in X
|
|
|
|
%Gaussian distribution using the data in X
|
|
|
|
% [mu sigma2] = estimateGaussian(X),
|
|
|
|
% [mu sigma2] = estimateGaussian(X),
|
|
|
|
% The input X is the dataset with each n-dimensional data point in one row
|
|
|
|
% The input X is the dataset with each n-dimensional data point in one row
|
|
|
|
% The output is an n-dimensional vector mu, the mean of the data set
|
|
|
|
% The output is an n-dimensional vector mu, the mean of the data set
|
|
|
|
% and the variances sigma^2, an n x 1 vector
|
|
|
|
% and the variances sigma^2, an n x 1 vector
|
|
|
|
%
|
|
|
|
%
|
|
|
|
|
|
|
|
|
|
|
|
% Useful variables
|
|
|
|
% Useful variables
|
|
|
|
[m, n] = size(X);
|
|
|
|
[m, n] = size(X);
|
|
|
@ -21,16 +21,9 @@ sigma2 = zeros(n, 1);
|
|
|
|
% should contain variance of the i-th feature.
|
|
|
|
% should contain variance of the i-th feature.
|
|
|
|
%
|
|
|
|
%
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mu = 1/m * sum(X)';
|
|
|
|
|
|
|
|
sigma2 = 1/m * sum(bsxfun(@minus, X, mu').^2)';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
% =============================================================
|
|
|
|
% =============================================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|