matthiasm@8: function smallT = marginalize_table(bigT, bigdom, bigsz, onto, maximize) matthiasm@8: % MARG_TABLE Marginalize a table matthiasm@8: % function smallT = marginalize_table(bigT, bigdom, bigsz, onto, maximize) matthiasm@8: matthiasm@8: % Like marg_table in BNT, except we do not assume the domains are sorted matthiasm@8: matthiasm@8: if nargin < 5, maximize = 0; end matthiasm@8: matthiasm@8: matthiasm@8: smallT = myreshape(bigT, bigsz); % make sure it is a multi-dim array matthiasm@8: sum_over = mysetdiff(bigdom, onto); matthiasm@8: ndx = find_equiv_posns(sum_over, bigdom); matthiasm@8: if maximize matthiasm@8: for i=1:length(ndx) matthiasm@8: smallT = max(smallT, [], ndx(i)); matthiasm@8: end matthiasm@8: else matthiasm@8: for i=1:length(ndx) matthiasm@8: smallT = sum(smallT, ndx(i)); matthiasm@8: end matthiasm@8: end matthiasm@8: matthiasm@8: matthiasm@8: ns = zeros(1, max(bigdom)); matthiasm@8: %ns(bigdom) = mysize(bigT); % ignores trailing dimensions of size 1 matthiasm@8: ns(bigdom) = bigsz; matthiasm@8: matthiasm@8: % If onto has a different ordering than bigdom, the following matthiasm@8: % will produce the wrong results matthiasm@8: matthiasm@8: %smallT = squeeze(smallT); % remove all dimensions of size 1 matthiasm@8: %smallT = myreshape(smallT, ns(onto)); % put back relevant dims of size 1 matthiasm@8: matthiasm@8: % so permute dimensions to match desired ordering (as specified by onto) matthiasm@8: matthiasm@8: matthiasm@8: % like find_equiv_posns, but keeps ordering matthiasm@8: outdom = [onto sum_over]; matthiasm@8: for i=1:length(outdom) matthiasm@8: j = find(bigdom==outdom(i)); matthiasm@8: match(i) = j; matthiasm@8: end matthiasm@8: outdom = [onto sum_over]; matthiasm@8: for i=1:length(outdom) matthiasm@8: j = find(bigdom==outdom(i)); matthiasm@8: match(i) = j; matthiasm@8: end matthiasm@8: if match ~= 1 matthiasm@8: smallT = permute(smallT, match); matthiasm@8: end