% mt_resample - Sample or resample melody triangle transition matrices of given size
%
% mt_resample ::
%    mt_system  ~'initial system'
%    natural    ~'size of transition matrices to resample'
% -> action mt_system.
%
% A new set of transition matrices will be sampled and a 3D information space
% scatter plot generated in the figure determined by the initial call to
% mt_init.

function Sys=mt_resample(Sys,K)
	figure(Sys.fig); 
	cla;


	L=Sys.L;
	tol=Sys.tol;
	II=zeros(L,3);
	ok=zeros(1,L);

	title(sprintf('sampling %d transition matrices...',L)); drawnow;
	TT=Sys.sample_transmats(K,L);
	title(sprintf('checking (tolerance=%f)...',tol)); drawnow;
	for i=1:L, [II(i,:),ok(i)]=mc_global_info(TT(:,:,i)); end
	todo=find(~ok);

	if Sys.ergmeth==1
		while ~isempty(todo)
			title(sprintf('resampling %d nonergodic transition matrices...',length(todo)));
			drawnow;
			TT(:,:,todo)=Sys.sample_transmats(K,length(todo));
			for i=todo, [II(i,:),ok(i)]=mc_global_info1(TT(:,:,i),tol); end
			todo=find(~ok);
		end
	else
		while ~isempty(todo)
			title(sprintf('perturbing %d nonergodic transition matrices...',length(todo)));
			drawnow;
			for i=todo, j=randnat(K); TT(:,j,i)=stoch(TT(:,j,i)+0.1); end
			for i=todo, [II(i,:),ok(i)]=mc_global_info1(TT(:,:,i),tol); end
			todo=find(~ok);
		end
	end

	Sys.transmats{K} = TT;
	Sys.info{K}      = II;
	Sys.hScat{K}     = scatc(II/log(2),II(:,3)/log(2),16); 
	axis on; box on; grid off; 
	lc=[0.4,0.4,0.4];
	set(gca,'Color','none');
	set(gca,'XColor',lc);
	set(gca,'YColor',lc);
	set(gca,'ZColor',lc);
	xlabel('entropy rate');
	ylabel('redundancy');
	zlabel('pred-info rate');
	rotate3d on;
end




