annotate toolboxes/FullBNT-1.0.7/KPMtools/marginalize_table.m @ 0:e9a9cd732c1e tip

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