Mercurial > hg > camir-aes2014
annotate 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 |
rev | line source |
---|---|
wolffd@0 | 1 function [T,Z] = mk_stochastic(T) |
wolffd@0 | 2 % MK_STOCHASTIC Ensure the argument is a stochastic matrix, i.e., the sum over the last dimension is 1. |
wolffd@0 | 3 % [T,Z] = mk_stochastic(T) |
wolffd@0 | 4 % |
wolffd@0 | 5 % If T is a vector, it will sum to 1. |
wolffd@0 | 6 % If T is a matrix, each row will sum to 1. |
wolffd@0 | 7 % If T is a 3D array, then sum_k T(i,j,k) = 1 for all i,j. |
wolffd@0 | 8 |
wolffd@0 | 9 % Set zeros to 1 before dividing |
wolffd@0 | 10 % This is valid since S(j) = 0 iff T(i,j) = 0 for all j |
wolffd@0 | 11 |
wolffd@0 | 12 if (ndims(T)==2) & (size(T,1)==1 | size(T,2)==1) % isvector |
wolffd@0 | 13 [T,Z] = normalise(T); |
wolffd@0 | 14 elseif ndims(T)==2 % matrix |
wolffd@0 | 15 Z = sum(T,2); |
wolffd@0 | 16 S = Z + (Z==0); |
wolffd@0 | 17 norm = repmat(S, 1, size(T,2)); |
wolffd@0 | 18 T = T ./ norm; |
wolffd@0 | 19 else % multi-dimensional array |
wolffd@0 | 20 ns = size(T); |
wolffd@0 | 21 T = reshape(T, prod(ns(1:end-1)), ns(end)); |
wolffd@0 | 22 Z = sum(T,2); |
wolffd@0 | 23 S = Z + (Z==0); |
wolffd@0 | 24 norm = repmat(S, 1, ns(end)); |
wolffd@0 | 25 T = T ./ norm; |
wolffd@0 | 26 T = reshape(T, ns); |
wolffd@0 | 27 end |