test_marg_ndxsd.m

来自「麻省理工学院的人工智能工具箱,很珍贵,希望对大家有用!」· M 代码 · 共 98 行

M
98
字号
% Here we give an example of how we can marginalize a tablefoo = 1;ns = 2*ones(1,20);if foo  bigdom = [3 4 7 8 10 11];  smalldom = [];else  bigdom = 1:5;  smalldom = [3 5];end% compute using dpotbigpot = dpot(bigdom, ns(bigdom), rand(ns(bigdom)));smallpot = marginalize_pot(bigpot, smalldom);S = struct(bigpot); bigT = S.T;S = struct(smallpot); smallT = S.T;if ~foofor c=1:C  for e=1:E    smallndx = subv2ind(smallsz, [c e]);    offset = (c-1)*4 + (e-1)*16;    s = 0;    for a=1:A      for b=1:B	for d=1:D	  bigndx = subv2ind(bigsz, [a b c d e]);	  base = subv2ind([A B C D E], [a b 1 d 1]) % clamp the outer loop to its 1st iteration	  assert(isequal(offset + base, bigndx))	  s = s + bigT(bigndx);	end      end    end    smallT2(smallndx) = s;  endendassert(approxeq(smallT, smallT2))i = 1;for c=1:C  for e=1:E    offset(i) = (c-1)*4 + (e-1)*16;    i = i + 1;  endend% This can be done faster by%%  c e% [0 0       [4%  1 0        16]%  0 1   * %  1 1]%% where 4 = A*B, 16 = A*B*C*Dend[small_ndx, diff_ndx] = mk_ndx(bigdom, smalldom, ns);% small_ndx = offset, diff_ndx = base-1S = length(small_ndx);D = length(diff_ndx);smallT4 = zeros(S,1);for i=1:S  s = 0;  for j=1:D    k = diff_ndx(j) + small_ndx(i) + 1;    s = s + bigT(k);  end  smallT4(i) = s;endassert(approxeq(smallT, smallT4))% Use 2D indicesndx = 1 + repmat(diff_ndx, S, 1) + repmat(small_ndx(:), 1, D); % ndx(i,j) = k abovefor i=1:S  s = 0;  for j=1:D    k = ndx(i,j);    s = s + bigT(k);  end  smallT5(i) = s;endassert(approxeq(smallT, smallT5))smallT6 = sum(bigT(ndx), 2);    assert(approxeq(smallT, smallT6))tmp.small = small_ndx;tmp.diff = diff_ndx;smallT7 = marg_table_ndx(bigT, 0, tmp);assert(approxeq(smallT, smallT7))

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?