% mt_get_transmat_at - Get transition matrix nearest given point in information space.
%
% mt_get_transmat_at ::
%    mt_system ~'Melody Triangle system structure',
%    natural   ~'voice Id (1 or greater)',
%    K:natural ~'size of transition matrix',
%    real      ~'X-coordinate',
%    real      ~'Y-coordinate'
% -> [[K,K]]   ~'transition matrix',
%    [[1,3]]   ~'information coordinates'.
%
% The selected transition matrix will be shown using mt_show_transmat,

function [T,P,I]=mt_get_transmat_at(Sys,Id,K,X,Y)
	logK=log(K);
	normpos = Sys.map([X;Y])';
	target = normpos*logK;
	tmats = Sys.transmats{K};
	info = Sys.info{K};
	L = size(info,1);

	% distance from target
	d2 = sum((info(:,1:2) - repmat(target,L,1)).^2,2);
	[dummy,J]=min(d2);
	T = tmats(:,:,J);
	I = info(J,:);
	mt_show_transmat(Id,T,I);
	if nargin>1, P=Sys.revmap(I(1:2)'/logK); end
end
