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 slpwscatter
Home > sltoolbox > stat > slpwscatter.m

slpwscatter

PURPOSE ^

SLPWSCATTER Compute the pairwise scatter matrix

SYNOPSIS ^

function S = slpwscatter(tar, W)

DESCRIPTION ^

SLPWSCATTER Compute the pairwise scatter matrix

 $ Syntax $
   - S = slpwscatter(X)
   - S = slpwscatter({X, Y})
   - S = slpwscatter(X, W)
   - S = slpwscatter({X, Y}, W)

 $ Description $
   - S = slpwscatter(X) computes the pairwise scatter matrix on the
     samples X using the following formula. Suppose X has m samples
     stored as columns, then 
       S = sum_{i=1:m} sum_{j=1:m} X(:,i) * X(:,j)'

   - S = slpwscatter({X, Y}) computes the pairwise scatter matrix on
     samples X and Y using the following formula. Suppose X has m samples
     and Y has n samples, then
       S = sum_{i=1:m} sum_{j=1:n} X(:,i) * X(:,j)'

   - S = slpwscatter(X, W) computes the weighted pairwise scatter matrix
     on the samples X using the following formula. Suppose X has m
     samples, then W should be an m x m matrix,
       S = sum_{i=1:m} sum_{j=1:m} W(i,j) X(:,i) * X(:,j)'

   - S = slpwscatter({X, Y}, W) computes the weighted pairwise scatter
     matrix on the samples X using the following formula. Suppose X has
     m samples and Y has n samples, then W should be an m x n matrix,
       S = sum_{i=1:m} sum_{j=1:n} W(i,j) X(:,i) * Y(:,j)'

 $ Remarks $
   - Instead of using the aforementioned formulas directly in computation,
     it converts the problem into matrix multiplication, thus much more
     efficient implementation can be applied.

 $ History $
   - Created by Dahua Lin on Apr 27, 2006

CROSS-REFERENCE INFORMATION ^

This function calls:
This function is called by:
  • slscatter SLSCATTER Compute the scatter matrix

SOURCE CODE ^

0001 function S = slpwscatter(tar, W)
0002 %SLPWSCATTER Compute the pairwise scatter matrix
0003 %
0004 % $ Syntax $
0005 %   - S = slpwscatter(X)
0006 %   - S = slpwscatter({X, Y})
0007 %   - S = slpwscatter(X, W)
0008 %   - S = slpwscatter({X, Y}, W)
0009 %
0010 % $ Description $
0011 %   - S = slpwscatter(X) computes the pairwise scatter matrix on the
0012 %     samples X using the following formula. Suppose X has m samples
0013 %     stored as columns, then
0014 %       S = sum_{i=1:m} sum_{j=1:m} X(:,i) * X(:,j)'
0015 %
0016 %   - S = slpwscatter({X, Y}) computes the pairwise scatter matrix on
0017 %     samples X and Y using the following formula. Suppose X has m samples
0018 %     and Y has n samples, then
0019 %       S = sum_{i=1:m} sum_{j=1:n} X(:,i) * X(:,j)'
0020 %
0021 %   - S = slpwscatter(X, W) computes the weighted pairwise scatter matrix
0022 %     on the samples X using the following formula. Suppose X has m
0023 %     samples, then W should be an m x m matrix,
0024 %       S = sum_{i=1:m} sum_{j=1:m} W(i,j) X(:,i) * X(:,j)'
0025 %
0026 %   - S = slpwscatter({X, Y}, W) computes the weighted pairwise scatter
0027 %     matrix on the samples X using the following formula. Suppose X has
0028 %     m samples and Y has n samples, then W should be an m x n matrix,
0029 %       S = sum_{i=1:m} sum_{j=1:n} W(i,j) X(:,i) * Y(:,j)'
0030 %
0031 % $ Remarks $
0032 %   - Instead of using the aforementioned formulas directly in computation,
0033 %     it converts the problem into matrix multiplication, thus much more
0034 %     efficient implementation can be applied.
0035 %
0036 % $ History $
0037 %   - Created by Dahua Lin on Apr 27, 2006
0038 %
0039 
0040 %% parse and verify input arguments
0041 
0042 % check target
0043 
0044 if isnumeric(tar)   % X
0045     
0046     X = tar;
0047     if ndims(X) ~= 2
0048         error('sltoolbox:invaliddims', ...
0049             'X should be a 2D matrix');
0050     end
0051     m = size(X, 2);    
0052     
0053     targettype = 1;
0054     
0055 elseif iscell(tar)  % {X, Y}
0056     
0057     if length(tar) ~= 2
0058         error('sltoolbox:invalidarg', ...
0059             'For cell target, it should be in the form {X, Y}');
0060     end
0061     
0062     X = tar{1};
0063     Y = tar{2};
0064     
0065     if ndims(X) ~= 2 || ndims(Y) ~= 2
0066         error('sltoolbox:invaliddims', ...
0067             'X and Y should be a 2D matrices');
0068     end
0069     
0070     if size(X, 1) ~= size(Y, 1)
0071         error('sltoolbox:sizmismatch', ...
0072             'The sample dimensions in X and Y are inconsistent');
0073     end
0074     
0075     m = size(X, 2);
0076     n = size(Y, 2);
0077     
0078     targettype = 2;
0079     
0080 else
0081     
0082     error('sltoolbox:invalidarg', ...
0083         'The tar should be either X or {X, Y}');
0084     
0085 end
0086 
0087 % check weight
0088 
0089 if nargin < 2
0090     W = [];
0091 end
0092 
0093 if ~isempty(W)    
0094     switch targettype        
0095         case 1
0096             if ~isequal(size(W), [m, m])
0097                 error('sltoolbox:sizmismatch', ...
0098                     'The weight matrix W should be an m x m matrix');
0099             end            
0100         case 2
0101             if ~isequal(size(W), [m, n])
0102                 error('sltoolbox:sizmismatch', ...
0103                     'The weight matrix W should be an m x n matrix');
0104             end            
0105     end    
0106 end
0107         
0108         
0109 %% compute
0110 
0111 switch targettype
0112     
0113     case 1          % X
0114    
0115         if isempty(W)   % not weighted
0116             D = diag(m(ones(m, 1)));
0117             W = ones(m, m);            
0118             M = 2 * (D - W);
0119             
0120             clear D W;
0121         else            % weighted
0122             D = diag(sum(W,1)' + sum(W,2));
0123             M = D - (W + W');
0124             
0125             clear D;
0126         end
0127         
0128         S = X * M * X';
0129                                                                                    
0130     case 2          % X, Y
0131         
0132         if isempty(W)   % not weighted
0133             S1 = X * diag(m(ones(m, 1))) * X';
0134             S2 = Y * diag(n(ones(n, 1))) * Y';
0135             S12 = S1 + S2;
0136             clear S1 S2;
0137             
0138             S3 = X * ones(m, n) * Y';
0139             S34 = S3 + S3';
0140             clear S3;
0141             
0142             S = S12 - S34;            
0143         else            % weighted
0144             S1 = X * diag(sum(W, 2)) * X';
0145             S2 = Y * diag(sum(W, 1)) * Y';
0146             S12 = S1 + S2;
0147             clear S1 S2;
0148             
0149             S3 = X * W * Y';
0150             S34 = S3 + S3';
0151             clear S3;
0152             
0153             S = S12 - S34;            
0154         end
0155             
0156                             
0157 end
0158 
0159 
0160 
0161     
0162     
0163     
0164     
0165     
0166

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

Contact us at files@mathworks.com