Daniel@0: function smallT = marg_table(bigT, bigdom, bigsz, onto, maximize) Daniel@0: % MARG_TABLE Marginalize a table Daniel@0: % smallT = marg_table(bigT, bigdom, bigsz, onto, maximize) Daniel@0: Daniel@0: if nargin < 5, maximize = 0; end Daniel@0: Daniel@0: Daniel@0: smallT = myreshape(bigT, bigsz); % make sure it is a multi-dim array Daniel@0: sum_over = mysetdiff(bigdom, onto); Daniel@0: ndx = find_equiv_posns(sum_over, bigdom); Daniel@0: if maximize Daniel@0: for i=1:length(ndx) Daniel@0: smallT = max(smallT, [], ndx(i)); Daniel@0: end Daniel@0: else Daniel@0: for i=1:length(ndx) Daniel@0: smallT = sum(smallT, ndx(i)); Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: Daniel@0: ns = zeros(1, max(bigdom)); Daniel@0: %ns(bigdom) = mysize(bigT); % ignores trailing dimensions of size 1 Daniel@0: ns(bigdom) = bigsz; Daniel@0: Daniel@0: smallT = squeeze(smallT); % remove all dimensions of size 1 Daniel@0: smallT = myreshape(smallT, ns(onto)); % put back relevant dims of size 1