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