Mercurial > hg > camir-aes2014
annotate toolboxes/FullBNT-1.0.7/KPMtools/logsumexp.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 s = logsumexp(a, dim) |
wolffd@0 | 2 % Returns log(sum(exp(a),dim)) while avoiding numerical underflow. |
wolffd@0 | 3 % Default is dim = 1 (rows) or dim=2 for a row vector |
wolffd@0 | 4 % logsumexp(a, 2) will sum across columns instead of rows |
wolffd@0 | 5 |
wolffd@0 | 6 % Written by Tom Minka, modified by Kevin Murphy |
wolffd@0 | 7 |
wolffd@0 | 8 if nargin < 2 |
wolffd@0 | 9 dim = 1; |
wolffd@0 | 10 if ndims(a) <= 2 & size(a,1)==1 |
wolffd@0 | 11 dim = 2; |
wolffd@0 | 12 end |
wolffd@0 | 13 end |
wolffd@0 | 14 |
wolffd@0 | 15 % subtract the largest in each column |
wolffd@0 | 16 [y, i] = max(a,[],dim); |
wolffd@0 | 17 dims = ones(1,ndims(a)); |
wolffd@0 | 18 dims(dim) = size(a,dim); |
wolffd@0 | 19 a = a - repmat(y, dims); |
wolffd@0 | 20 s = y + log(sum(exp(a),dim)); |
wolffd@0 | 21 %i = find(~finite(y)); |
wolffd@0 | 22 %if ~isempty(i) |
wolffd@0 | 23 % s(i) = y(i); |
wolffd@0 | 24 %end |