Daniel@0: function n2 = mdndist2(mixparams, t) Daniel@0: %MDNDIST2 Calculates squared distance between centres of Gaussian kernels and data Daniel@0: % Daniel@0: % Description Daniel@0: % N2 = MDNDIST2(MIXPARAMS, T) takes takes the centres of the Gaussian Daniel@0: % contained in MIXPARAMS and the target data matrix, T, and computes Daniel@0: % the squared Euclidean distance between them. If T has M rows and N Daniel@0: % columns, then the CENTRES field in the MIXPARAMS structure should Daniel@0: % have M rows and N*MIXPARAMS.NCENTRES columns: the centres in each row Daniel@0: % relate to the corresponding row in T. The result has M rows and Daniel@0: % MIXPARAMS.NCENTRES columns. The I, Jth entry is the squared distance Daniel@0: % from the Ith row of X to the Jth centre in the Ith row of Daniel@0: % MIXPARAMS.CENTRES. Daniel@0: % Daniel@0: % See also Daniel@0: % MDNFWD, MDNPROB Daniel@0: % Daniel@0: Daniel@0: % Copyright (c) Ian T Nabney (1996-2001) Daniel@0: % David J Evans (1998) Daniel@0: Daniel@0: % Check arguments for consistency Daniel@0: errstring = consist(mixparams, 'mdnmixes'); Daniel@0: if ~isempty(errstring) Daniel@0: error(errstring); Daniel@0: end Daniel@0: Daniel@0: ncentres = mixparams.ncentres; Daniel@0: dim_target = mixparams.dim_target; Daniel@0: ntarget = size(t, 1); Daniel@0: if ntarget ~= size(mixparams.centres, 1) Daniel@0: error('Number of targets does not match number of mixtures') Daniel@0: end Daniel@0: if size(t, 2) ~= mixparams.dim_target Daniel@0: error('Target dimension does not match mixture dimension') Daniel@0: end Daniel@0: Daniel@0: % Build t that suits parameters, that is repeat t for each centre Daniel@0: t = kron(ones(1, ncentres), t); Daniel@0: Daniel@0: % Do subtraction and square Daniel@0: diff2 = (t - mixparams.centres).^2; Daniel@0: Daniel@0: % Reshape and sum each component Daniel@0: diff2 = reshape(diff2', dim_target, (ntarget*ncentres))'; Daniel@0: n2 = sum(diff2, 2); Daniel@0: Daniel@0: % Calculate the sum of distance, and reshape Daniel@0: % so that we have a distance for each centre per target Daniel@0: n2 = reshape(n2, ncentres, ntarget)'; Daniel@0: