comparison toolboxes/FullBNT-1.0.7/KPMtools/mk_stochastic.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e9a9cd732c1e
1 function [T,Z] = mk_stochastic(T)
2 % MK_STOCHASTIC Ensure the argument is a stochastic matrix, i.e., the sum over the last dimension is 1.
3 % [T,Z] = mk_stochastic(T)
4 %
5 % If T is a vector, it will sum to 1.
6 % If T is a matrix, each row will sum to 1.
7 % If T is a 3D array, then sum_k T(i,j,k) = 1 for all i,j.
8
9 % Set zeros to 1 before dividing
10 % This is valid since S(j) = 0 iff T(i,j) = 0 for all j
11
12 if (ndims(T)==2) & (size(T,1)==1 | size(T,2)==1) % isvector
13 [T,Z] = normalise(T);
14 elseif ndims(T)==2 % matrix
15 Z = sum(T,2);
16 S = Z + (Z==0);
17 norm = repmat(S, 1, size(T,2));
18 T = T ./ norm;
19 else % multi-dimensional array
20 ns = size(T);
21 T = reshape(T, prod(ns(1:end-1)), ns(end));
22 Z = sum(T,2);
23 S = Z + (Z==0);
24 norm = repmat(S, 1, ns(end));
25 T = T ./ norm;
26 T = reshape(T, ns);
27 end