Daniel@0: function s = logsumexpv(a) Daniel@0: % Returns log(sum(exp(a))) while avoiding numerical underflow. Daniel@0: % Daniel@0: % e.g., log(e^a1 + e^a2) = a1 + log(1 + e^(a2-a1)) if a1>a2 Daniel@0: % If a1 ~ a2, and a1>a2, then e^(a2-a1) is exp(small negative number), Daniel@0: % which can be computed without underflow. Daniel@0: Daniel@0: % Same as logsumexp, except we assume a is a vector. Daniel@0: % This avoids a call to repmat, which takes 50% of the time! Daniel@0: Daniel@0: a = a(:)'; % make row vector Daniel@0: m = max(a); Daniel@0: b = a - m*ones(1,length(a)); Daniel@0: s = m + log(sum(exp(b))); Daniel@0: