Mercurial > hg > mauch-mirex-2010
annotate _FullBNT/KPMtools/mk_stochastic.m @ 9:4ea6619cb3f5 tip
removed log files
author | matthiasm |
---|---|
date | Fri, 11 Apr 2014 15:55:11 +0100 |
parents | b5b38998ef3b |
children |
rev | line source |
---|---|
matthiasm@8 | 1 function [T,Z] = mk_stochastic(T) |
matthiasm@8 | 2 % MK_STOCHASTIC Ensure the argument is a stochastic matrix, i.e., the sum over the last dimension is 1. |
matthiasm@8 | 3 % [T,Z] = mk_stochastic(T) |
matthiasm@8 | 4 % |
matthiasm@8 | 5 % If T is a vector, it will sum to 1. |
matthiasm@8 | 6 % If T is a matrix, each row will sum to 1. |
matthiasm@8 | 7 % If T is a 3D array, then sum_k T(i,j,k) = 1 for all i,j. |
matthiasm@8 | 8 |
matthiasm@8 | 9 % Set zeros to 1 before dividing |
matthiasm@8 | 10 % This is valid since S(j) = 0 iff T(i,j) = 0 for all j |
matthiasm@8 | 11 |
matthiasm@8 | 12 if (ndims(T)==2) & (size(T,1)==1 | size(T,2)==1) % isvector |
matthiasm@8 | 13 [T,Z] = normalise(T); |
matthiasm@8 | 14 elseif ndims(T)==2 % matrix |
matthiasm@8 | 15 Z = sum(T,2); |
matthiasm@8 | 16 S = Z + (Z==0); |
matthiasm@8 | 17 norm = repmat(S, 1, size(T,2)); |
matthiasm@8 | 18 T = T ./ norm; |
matthiasm@8 | 19 else % multi-dimensional array |
matthiasm@8 | 20 ns = size(T); |
matthiasm@8 | 21 T = reshape(T, prod(ns(1:end-1)), ns(end)); |
matthiasm@8 | 22 Z = sum(T,2); |
matthiasm@8 | 23 S = Z + (Z==0); |
matthiasm@8 | 24 norm = repmat(S, 1, ns(end)); |
matthiasm@8 | 25 T = T ./ norm; |
matthiasm@8 | 26 T = reshape(T, ns); |
matthiasm@8 | 27 end |