function seq = sample_dbn(bnet, T)
% SAMPLE_DBN Generate a random sequence from a DBN.
%
% SEQ = SAMPLE_DBN(BNET, T) returns a matrix with T columns. SEQ(:,t) contains the values of all
% the nodes in slice t concatenated together.

bnet.values = blockmatrix(1, [], 1, [bnet.blocks_slice(:)' bnet.blocks_slice(:)']); % room for 2 slices
%%% bnet.values = blockmatrix(1, [bnet.blocks_slice(:)' bnet.blocks_slice(:)']); % room for 2 slices
ss = bnet.slice_size; % num nodes in a slice
t = 1;
seq = blockmatrix([], T, bnet.blocks_slice, 1);
%%%seq = blockmatrix(bnet.blocks_slice, T);
for i=1:ss
  e = bnet.equiv_class_slice1(i);
  x =  sample_bnode(bnet.bnodes{e}, bnet);
  bnet.values(i) = x;
  seq(i,t) = x;
end
for t=2:T
  for i=1:ss
    e = bnet.equiv_class_slice2(i);
    x =  sample_bnode(bnet.bnodes{e}, bnet);
    % The nodes in slice t (t >= 2) think their parents are in slices 1 and 2
    bnet.values(i+ss) = x;
    seq(i,t) = x;
  end
  % Move the slice 2 data back to slice 1 to make room
  bnet.values(1:ss) = bnet.values((ss+1):(2*ss));
end

