samer@4: function [Y,Z]=stochastic1(X), samer@4: % stochastic1 - make columns sum to one (like probabilities) samer@4: % samer@4: % stochastic1 :: [[N,M]] -> [[N,M]], [[M]]. samer@4: % samer@4: % optionally returns the column sums as well. samer@4: % NOTE: this function assumes that X contains no NaNs and samer@4: % at most one Inf per column. Eg, the function will map samer@4: % samer@4: % [2;3;6;Inf;1;8] -> [0;0;0;1;0;0] samer@4: % samer@4: % See also: the function stochastic works on ROWS not columns. samer@4: samer@4: samer@4: Z=sum(X,1); samer@4: samer@4: Y=X./repmat(max(Z,realmin),size(X,1),1); samer@4: samer@4: % any nans are assumed to be inf/inf so are replaced with 1 samer@4: Y(isnan(Y))=1; samer@4: