function [Y,Z]=stochastic(X),
% stochastic - make rows sum to one (like probabilities)
%              optionally returns the row sums as well
%
% stochastic :: [[N,M]] -> [[N,M]], [[N]].

Z=sum(X,2);
Y=X./repmat(max(Z,realmin),1,size(X,2));

