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