diff general/numerical/stochastic1.m @ 4:e44f49929e56

Adding reorganised general toolbox, now in several subdirectories.
author samer
date Sat, 12 Jan 2013 19:21:22 +0000
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/general/numerical/stochastic1.m	Sat Jan 12 19:21:22 2013 +0000
@@ -0,0 +1,21 @@
+function [Y,Z]=stochastic1(X),
+% stochastic1 - make columns sum to one (like probabilities)
+%
+% stochastic1 :: [[N,M]] -> [[N,M]], [[M]].
+%
+% optionally returns the column sums as well. 
+% NOTE: this function assumes that X contains no NaNs and
+% at most one Inf per column. Eg, the function will map
+%
+% [2;3;6;Inf;1;8] -> [0;0;0;1;0;0]
+%
+% See also: the function stochastic works on ROWS not columns.
+
+
+Z=sum(X,1);
+
+Y=X./repmat(max(Z,realmin),size(X,1),1);
+
+% any nans are assumed to be inf/inf so are replaced with 1
+Y(isnan(Y))=1;
+