comparison toolboxes/FullBNT-1.0.7/KPMtools/logsumexp.m @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:cc4b1211e677
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