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