Code covered by the BSD License  

Highlights from
Statistical Learning Toolbox

from Statistical Learning Toolbox by Dahua Lin
Functions for statistical learning, pattern recognition and computer vision, covering many topics.

Description of slgaussmdist
Home > sltoolbox > stat > slgaussmdist.m

slgaussmdist

PURPOSE ^

SLGAUSSMDIST Computes the Malanobis distance between samples and centers

SYNOPSIS ^

function dists = slgaussmdist(GS, X)

DESCRIPTION ^

SLGAUSSMDIST Computes the Malanobis distance between samples and centers

 $ Syntax $
   - dists = slgaussmdist(GS, X)

 $ Arguments $
   - GS:       the Gaussian models
   - X:        the sample matrix
   - dists:    the distances of samples to the model centers
       
 $ Description $
   - dists = slgaussmdist(GS, X) computes the distances from the samples
     in X and the model centers of GS. If there are n samples in X, and
     k models in GS, then dists is a k x n matrix. Each column of dists
     is the distances from the corresponding sample to all model centers.
     The distances for each model is computed based on the variances or
     covariances of that model.
 
 $ History $
   - Created by Dahua Lin, on Aug 28, 2006

CROSS-REFERENCE INFORMATION ^

This function calls:
  • slmetric_pw SLMETRIC_PW Compute the metric between column vectors pairwisely
  • slgausstype SLGAUSSTYPE Judges the type of a Gaussian model struct
  • raise_lackinput RAISE_LACKINPUT Raises an error indicating lack of input argument
This function is called by:
  • slgausspdf SLGAUSSPDF Computes the probability density of Gaussian models

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function dists = slgaussmdist(GS, X)
0002 %SLGAUSSMDIST Computes the Malanobis distance between samples and centers
0003 %
0004 % $ Syntax $
0005 %   - dists = slgaussmdist(GS, X)
0006 %
0007 % $ Arguments $
0008 %   - GS:       the Gaussian models
0009 %   - X:        the sample matrix
0010 %   - dists:    the distances of samples to the model centers
0011 %
0012 % $ Description $
0013 %   - dists = slgaussmdist(GS, X) computes the distances from the samples
0014 %     in X and the model centers of GS. If there are n samples in X, and
0015 %     k models in GS, then dists is a k x n matrix. Each column of dists
0016 %     is the distances from the corresponding sample to all model centers.
0017 %     The distances for each model is computed based on the variances or
0018 %     covariances of that model.
0019 %
0020 % $ History $
0021 %   - Created by Dahua Lin, on Aug 28, 2006
0022 %
0023 
0024 %% parse and verify input arguments
0025 
0026 if nargin < 2
0027     raise_lackinput('slgaussmdist', 2);
0028 end
0029 
0030 if ~isnumeric(X) || ndims(X) ~= 2
0031     error('sltoolbox:invalidarg', ...
0032         'The X should be a 2D numeric matrix');
0033 end
0034 
0035 tyi = slgausstype(GS);
0036 
0037 if ~tyi.hasinv
0038     error('sltoolbox:invalidarg', ...
0039         'GS should have inverse variance/covariance computed');
0040 end
0041 
0042 [d, n] = size(X);
0043 if d ~= GS.dim
0044     error('sltoolbox:sizmismatch', ...
0045         'The dimension of samples does not match that of the models');
0046 end
0047 
0048 k = GS.nmodels;
0049 
0050 %% Main skeleton
0051 
0052 switch tyi.varform
0053     case 'univar'
0054         if tyi.sharevar
0055             dists = compmdist_univar(GS.means, X, GS.invvars);
0056         else
0057             dists = zeros(k, n);
0058             for i = 1 : k
0059                 dists(i, :) = compmdist_univar(GS.means(:,i), X, GS.invvars(i));
0060             end
0061         end
0062     case 'diagvar'
0063         if tyi.sharevar
0064             dists = compmdist_diagvar(GS.means, X, GS.invvars);
0065         else
0066             dists = zeros(k, n);
0067             for i = 1 : k
0068                 dists(i, :) = compmdist_diagvar(GS.means(:,i), X, GS.invvars(:,i));
0069             end
0070         end
0071     case 'covar'
0072         if tyi.sharevar
0073             dists = compmdist_covar(GS.means, X, GS.invcovs);
0074         else
0075             dists = zeros(k, n);
0076             for i = 1 : k
0077                 dists(i, :) = compmdist_covar(GS.means(:,i), X, GS.invcovs(:,:,i));
0078             end
0079         end
0080 end
0081 
0082 
0083 %% Core computation routines
0084 
0085 function dists = compmdist_univar(M, X, invvar)
0086 
0087 dists = slmetric_pw(M, X, 'sqdist');
0088 dists = dists * invvar;
0089 dists = sqrt(max(dists, 0));
0090 
0091 function dists = compmdist_diagvar(M, X, invvars)
0092 
0093 dists = slmetric_pw(M, X, 'wsqdist', invvars);
0094 dists = sqrt(max(dists, 0));
0095 
0096 function dists = compmdist_covar(M, X, invcovs)
0097 
0098 dists = slmetric_pw(M, X, 'quaddiff', invcovs);
0099 dists = sqrt(max(dists, 0));
0100 
0101 
0102 
0103 
0104 
0105

Generated on Wed 20-Sep-2006 12:43:11 by m2html © 2003

Contact us at files@mathworks.com