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 sl2dmatcov
Home > sltoolbox > subspace_ex > sl2dmatcov.m

sl2dmatcov

PURPOSE ^

SL2DMATCOV Computes the 2D matrix-covariances

SYNOPSIS ^

function varargout = sl2dmatcov(type, data, matsiz, n, meanmat, PL, PR, w)

DESCRIPTION ^

SL2DMATCOV Computes the 2D matrix-covariances

 $ Syntax $
   - CL = sl2dmatcov('CL', data, matsiz, n, meanmat, PL, PR, w)
   - CR = sl2dmatcov('CR', data, matsiz, n, meanmat, PL, PR, w)
   - [CL, CR] = sl2dmatcov('Both', data, matsiz, n, meanmat, PL, PR, w)

 $ Arguments $
   - data:     the stack of matrices or the cell array of filenames of the
               array files storing the matrices.
   - matsiz:   the size of each matrix
   - n:        the number of samples
   - PL:       the left-projection matrix, default = []
   - PR:       the right-projection matrix, default = []
   - w:        the weights of the matrix samples, default = []

 $ Remarks $
   - The function computes the 2D matrix-covariances according to 
     the following formulas:
       Y_i = PL^T * (X - meanX)* PR 
       CL = ( sum_{i=1}^n w(i) * Y_i * Y_i^T ) / ( sum_{i=1}^n w(i) )
       CR = ( sum_{i=1}^n w(i) * Y_i^T * Y_i ) / ( sum_{i=1}^n w(i) )
     Following special cases are considered:
       If w is empty, then we take w(i) = 1 for all i
       If PL is empty, then we take PL as an identity matrix
       If PR is empty, then we take PR as an identity matrix
       If meanmat is 0, then we take meanmat as a zero matrix

 $ History $
   - Created by Dahua Lin, on Jul 31st, 2006

CROSS-REFERENCE INFORMATION ^

This function calls:
  • slreadarray SLREADARRAY Reads an array from an array file
  • raise_lackinput RAISE_LACKINPUT Raises an error indicating lack of input argument
This function is called by:
  • sl2dpcaex SL2DPCAEX Learns Extended 2D PCA on a set of matrix samples

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function varargout = sl2dmatcov(type, data, matsiz, n, meanmat, PL, PR, w)
0002 %SL2DMATCOV Computes the 2D matrix-covariances
0003 %
0004 % $ Syntax $
0005 %   - CL = sl2dmatcov('CL', data, matsiz, n, meanmat, PL, PR, w)
0006 %   - CR = sl2dmatcov('CR', data, matsiz, n, meanmat, PL, PR, w)
0007 %   - [CL, CR] = sl2dmatcov('Both', data, matsiz, n, meanmat, PL, PR, w)
0008 %
0009 % $ Arguments $
0010 %   - data:     the stack of matrices or the cell array of filenames of the
0011 %               array files storing the matrices.
0012 %   - matsiz:   the size of each matrix
0013 %   - n:        the number of samples
0014 %   - PL:       the left-projection matrix, default = []
0015 %   - PR:       the right-projection matrix, default = []
0016 %   - w:        the weights of the matrix samples, default = []
0017 %
0018 % $ Remarks $
0019 %   - The function computes the 2D matrix-covariances according to
0020 %     the following formulas:
0021 %       Y_i = PL^T * (X - meanX)* PR
0022 %       CL = ( sum_{i=1}^n w(i) * Y_i * Y_i^T ) / ( sum_{i=1}^n w(i) )
0023 %       CR = ( sum_{i=1}^n w(i) * Y_i^T * Y_i ) / ( sum_{i=1}^n w(i) )
0024 %     Following special cases are considered:
0025 %       If w is empty, then we take w(i) = 1 for all i
0026 %       If PL is empty, then we take PL as an identity matrix
0027 %       If PR is empty, then we take PR as an identity matrix
0028 %       If meanmat is 0, then we take meanmat as a zero matrix
0029 %
0030 % $ History $
0031 %   - Created by Dahua Lin, on Jul 31st, 2006
0032 %
0033 
0034 %% parse and verify input arguments
0035 
0036 if nargin < 5
0037     raise_lackinput('sl2dmatcov', 5);
0038 end
0039 if nargin < 6
0040     PL = [];
0041 end
0042 if nargin < 7
0043     PR = [];
0044 end
0045 if nargin < 8
0046     w = [];
0047 end
0048 
0049 matsiz = matsiz(:)';
0050 if length(matsiz) ~= 2
0051     error('sltoolbox:invalidarg', ...
0052         'matsiz shoudl be a 2-elem vector');
0053 end
0054 if ~isequal(size(meanmat), matsiz)
0055     error('sltoolbox:invalidarg', ...
0056         'The size of mean matrix is not as specified');
0057 end
0058 
0059 d1 = matsiz(1);
0060 d2 = matsiz(2);
0061 if ~isempty(PL) && size(PL, 1) ~= d1
0062     error('sltoolbox:invalidarg', ...
0063         'The size of PL is inconsistent with the matrix dimension');
0064 end
0065 if ~isempty(PR) && size(PR, 1) ~= d2
0066     error('sltoolbox:invalidarg', ...
0067         'The size of PR is inconsistent with the matrix dimension');
0068 end
0069 
0070 if ~isempty(w)
0071     if length(w) ~= n
0072         error('The weights length is inconsistent with the number of samples');
0073     end
0074     tw = sum(w);
0075 else
0076     tw = n;
0077 end
0078 
0079 
0080 %% Main body
0081 
0082 if isnumeric(data)
0083 
0084     if ~isequal(size(data), [matsiz, n])
0085         error('sltoolbox:invalidarg', ...
0086             'The size of data is inconsistent with specified');
0087     end
0088     
0089     Y = compute_Y(data, meanmat, PL, PR);
0090     
0091     switch type
0092         case 'CL'
0093             CL = compute_SL(Y, w);
0094             CL = CL / tw;
0095             varargout = {CL};
0096         case 'CR'
0097             CR = compute_SR(Y, w);
0098             CR = CR / tw;
0099             varargout = {CR};
0100         case 'Both'
0101             [CL, CR] = compute_SLSR(Y, w);
0102             CL = CL / tw;
0103             CR = CR / tw;
0104             varargout = {CL, CR};
0105         otherwise
0106             error('sltoolbox:invalidarg', ...
0107                 'invalid type: %s', type);
0108     end            
0109     
0110     
0111 elseif iscell(data)
0112     
0113     nfiles = length(data);
0114     cf = 0;
0115     
0116     switch type
0117         case 'CL'
0118             for i = 1 : nfiles
0119                 curarr = slreadarray(data{i});
0120                 curn = size(curarr, 3);
0121                 Y = compute_Y(curarr, meanmat, PL, PR);
0122                 curCL = compute_SL(Y, w); 
0123                 if i == 1
0124                     CL = curCL;
0125                 else
0126                     CL = CL + curCL;
0127                 end
0128                 clear Y curCL;
0129                 cf = cf + curn;
0130             end
0131             CL = CL / tw;
0132             varargout = {CL};
0133             
0134         case 'CR'
0135             for i = 1 : nfiles
0136                 curarr = slreadarray(data{i});
0137                 curn = size(curarr, 3);
0138                 Y = compute_Y(curarr, meanmat, PL, PR);
0139                 curCR = compute_SR(Y, w); 
0140                 if i == 1
0141                     CR = curCR;
0142                 else
0143                     CR = CR + curCR;
0144                 end
0145                 clear Y curCR;
0146                 cf = cf + curn;
0147             end
0148             CR = CR / tw;
0149             varargout = {CR};
0150             
0151         case 'Both'
0152             for i = 1 : nfiles
0153                 curarr = slreadarray(data{i});
0154                 curn = size(curarr, 3);
0155                 Y = compute_Y(curarr, meanmat, PL, PR);
0156                 [curCL, curCR] = compute_SLSR(Y, w); 
0157                 if i == 1
0158                     CL = curCL;
0159                     CR = curCR;
0160                 else
0161                     CL = CL + curCL;
0162                     CR = CR + curCR;
0163                 end
0164                 clear Y curCL curCR;
0165                 cf = cf + curn;
0166             end
0167             CL = CL / tw;
0168             CR = CR / tw;
0169             varargout = {CL, CR};
0170             
0171         otherwise
0172             error('sltoolbox:invalidarg', ...
0173                 'invalid type: %s', type);        
0174     end
0175     
0176     if cf ~= n
0177         error('sltoolbox:sizmismatch', ...
0178             'The total number of samples is not n');
0179     end
0180             
0181 else
0182     error('sltoolbox:invalidarg', ...
0183         'data should be a numeric array or a cell array of filenames');    
0184 end
0185 
0186 
0187 
0188 %% Core routine
0189 
0190 function Y = compute_Y(X, M, PL, PR)
0191 
0192 n = size(X, 3);
0193 if isempty(PL)
0194     if isempty(PR)
0195         if isequal(M, 0)
0196             Y = X;
0197         else
0198             Y = zeros(size(X, 1), size(X, 2), n);
0199             for i = 1 : n
0200                 Y(:,:,i) = X(:,:,i) - M;
0201             end
0202         end
0203     else
0204         Y = zeros(size(X, 1), size(PR, 2), n);
0205         if isequal(M, 0)           
0206             for i = 1 : n
0207                 Y(:,:,i) = X(:,:,i) * PR;
0208             end
0209         else
0210             for i = 1 : n
0211                 Y(:,:,i) = (X(:,:,i) - M) * PR;
0212             end
0213         end
0214     end
0215 else
0216     PLT = PL';
0217     if isempty(PR)
0218         Y = zeros(size(PL, 2), size(X, 2), n);
0219         if isequal(M, 0)
0220             for i = 1 : n
0221                 Y(:,:,i) = PLT * X(:,:,i);
0222             end
0223         else
0224             for i = 1 : n
0225                 Y(:,:,i) = PLT * (X(:,:,i) - M);
0226             end
0227         end
0228     else
0229         Y = zeros(size(PL, 2), size(PR, 2), n);
0230         if isequal(M, 0)
0231             for i = 1 : n
0232                 Y(:,:,i) = PLT * X(:,:,i) * PR;
0233             end
0234         else
0235             for i = 1 : n
0236                 Y(:,:,i) = PLT * (X(:,:,i) - M) * PR;
0237             end
0238         end
0239     end
0240 end
0241 
0242 
0243 function SL = compute_SL(Y, w)
0244 
0245 SL = zeros(size(Y, 1));
0246 n = size(Y, 3);
0247 if isempty(w)
0248     for i = 1 : n
0249         curY = Y(:, :, i);
0250         SL = SL + curY * curY';
0251     end
0252 else
0253     for i = 1 : n
0254         curY = Y(:, :, i);
0255         SL = SL + w(i) * curY * curY';
0256     end
0257 end
0258 
0259 function SR = compute_SR(Y, w)
0260 
0261 SR = zeros(size(Y, 2));
0262 n = size(Y, 3);
0263 if isempty(w)
0264     for i = 1 : n
0265         curY = Y(:, :, i);
0266         SR = SR + curY' * curY;
0267     end
0268 else
0269     for i = 1 : n
0270         curY = Y(:, :, i);
0271         SR = SR + w(i) * curY' * curY;
0272     end
0273 end    
0274 
0275 function [SL, SR] = compute_SLSR(Y, w)
0276 
0277 SL = zeros(size(Y, 1));
0278 SR = zeros(size(Y, 2));
0279 n = size(Y, 3);
0280 if isempty(w)
0281     for i = 1 : n
0282         curY = Y(:, :, i);
0283         SL = SL + curY * curY';
0284         SR = SR + curY' * curY;
0285     end
0286 else
0287     for i = 1 : n
0288         curY = Y(:, :, i);
0289         SL = SL + w(i) * curY * curY';
0290         SR = SR + w(i) * curY' * curY;
0291     end
0292 end
0293     
0294

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

Contact us at files@mathworks.com