annotate _FullBNT/KPMtools/marginalize_table.m @ 9:4ea6619cb3f5 tip

removed log files
author matthiasm
date Fri, 11 Apr 2014 15:55:11 +0100
parents b5b38998ef3b
children
rev   line source
matthiasm@8 1 function smallT = marginalize_table(bigT, bigdom, bigsz, onto, maximize)
matthiasm@8 2 % MARG_TABLE Marginalize a table
matthiasm@8 3 % function smallT = marginalize_table(bigT, bigdom, bigsz, onto, maximize)
matthiasm@8 4
matthiasm@8 5 % Like marg_table in BNT, except we do not assume the domains are sorted
matthiasm@8 6
matthiasm@8 7 if nargin < 5, maximize = 0; end
matthiasm@8 8
matthiasm@8 9
matthiasm@8 10 smallT = myreshape(bigT, bigsz); % make sure it is a multi-dim array
matthiasm@8 11 sum_over = mysetdiff(bigdom, onto);
matthiasm@8 12 ndx = find_equiv_posns(sum_over, bigdom);
matthiasm@8 13 if maximize
matthiasm@8 14 for i=1:length(ndx)
matthiasm@8 15 smallT = max(smallT, [], ndx(i));
matthiasm@8 16 end
matthiasm@8 17 else
matthiasm@8 18 for i=1:length(ndx)
matthiasm@8 19 smallT = sum(smallT, ndx(i));
matthiasm@8 20 end
matthiasm@8 21 end
matthiasm@8 22
matthiasm@8 23
matthiasm@8 24 ns = zeros(1, max(bigdom));
matthiasm@8 25 %ns(bigdom) = mysize(bigT); % ignores trailing dimensions of size 1
matthiasm@8 26 ns(bigdom) = bigsz;
matthiasm@8 27
matthiasm@8 28 % If onto has a different ordering than bigdom, the following
matthiasm@8 29 % will produce the wrong results
matthiasm@8 30
matthiasm@8 31 %smallT = squeeze(smallT); % remove all dimensions of size 1
matthiasm@8 32 %smallT = myreshape(smallT, ns(onto)); % put back relevant dims of size 1
matthiasm@8 33
matthiasm@8 34 % so permute dimensions to match desired ordering (as specified by onto)
matthiasm@8 35
matthiasm@8 36
matthiasm@8 37 % like find_equiv_posns, but keeps ordering
matthiasm@8 38 outdom = [onto sum_over];
matthiasm@8 39 for i=1:length(outdom)
matthiasm@8 40 j = find(bigdom==outdom(i));
matthiasm@8 41 match(i) = j;
matthiasm@8 42 end
matthiasm@8 43 outdom = [onto sum_over];
matthiasm@8 44 for i=1:length(outdom)
matthiasm@8 45 j = find(bigdom==outdom(i));
matthiasm@8 46 match(i) = j;
matthiasm@8 47 end
matthiasm@8 48 if match ~= 1
matthiasm@8 49 smallT = permute(smallT, match);
matthiasm@8 50 end