function Theta = nkj2theta(Nkj,alphas)
% Convert topic counts per document into topic probabilities per document

% Inputs:
%   Nkj     - K-by-N matrix of topic counts per document for K topics and N
%             documents, double
%   alphas  - Dirichlet prior, K-by-1 double vector
%
% Output:
%   Theta   - K-by-N matrix of topic probabilities per document for K
%             topics and N documents, double

%   Copyright 2017 The MathWorks, Inc.

nj = sum(Nkj,1);

alpha0 = sum(alphas);

Theta = (Nkj + alphas)./(nj + alpha0);
if alpha0==0
    bad = nj==0;
    Theta(:,bad) = 0;
end

end
