function [y,posteriors] = predictgaussians(x, means, sigmas, priors)
% [y,posteriors] = predictgaussians(x, means, sigmas, priors)
% Returns in Y the labels of examples in X predicted from posteriors,
% estimated with Gaussian generative model using rows of MEANS as means,
% and matrices stacked on top  of each other in SIGMAS as covariance
% matrices. PRIORS is optional (default is uniform priors over all
% classes).
% The posteriors used in the prediction can be fetched as the second
% output argument.
% Example: for two 2D Gaussians with means  mu1.mu2 and covariance
% matrices c1,c2 respectively, the call shoud be
% y = predictgaussians(x,[mu1;mu2],[c1;c2]);
%
% See also: multigaussian    plotgaussians 

k = size(means,1);
if (nargin < 4)
  priors = ones(1,k)/k;
end


  [n,d] = size(x);
  posteriors = zeros(n,k);
  for i=1:k
      posteriors(:,i) = priors(i)*multigaussian(x,means(i,:),sigmas(d*(i-1)+1:d*i,:));
  end
  [dummy, y] = max(posteriors,[],2);
  y = y-1;  % Correct because class labels start with 0
  
