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

slmean

PURPOSE ^

SLMEAN Compute the mean vector of samples

SYNOPSIS ^

function v = slmean(M, w, hasbeenchecked)

DESCRIPTION ^

SLMEAN Compute the mean vector of samples

 $ Syntax $
   - v = slmean(M)
   - v = slmean(M, w)
   - v = slmean(M, w, true)

 $ Arguments $
   - M:        the matrix of sample vectors stored as columns
   - w:        the weights of the vectors
   - v:        the computed mean vector

 $ Description $
   - v = slmean(M) computes the mean vector of column vectors in M.
   
   - v = slmean(M, w) computes the weighted mean vector of column vectors
     in M. w is the weights of the samples. if w is empty, the normal
     mean vector would be computed. 

   - v = slmean(M, w, true) indicates that the size consistency has been
     checked by invoker. Then in this function, it will not be checked
     again. This syntax is designed for the sake of efficiency.

 $ Remarks $
   - M should be a 2D matrix (d x n), then w should be a 1 x n row vector,
     v would be a d x 1 column vector.

 $ History $
   - Created by Dahua Lin on Apr 22nd, 2006
   - Modified by Dahua Lin on Sep 10th, 2006
       - replace slmul by slmulvec to increase efficiency

CROSS-REFERENCE INFORMATION ^

This function calls:
  • slmulvec SLMULVEC multiplies a vector to columns or rows of a matrix
This function is called by:
  • slkmeans SLKMEANS Performs K-Means Clustering on samples
  • sldistmean SLDISTMEAN Uses fast method to compute means of pairwise distances
  • sllocaltanspace SLLOCALTANSPACE Solves the local tangent spaces
  • slcov SLCOV Compute the covariance matrix
  • slcovlarge SLCOVLARGE Computes large covariance matrix using memory-efficient way
  • slgaussest SLGAUSSEST Estimates the Gaussian models from samples
  • slmeans SLMEANS Compute the mean vectors
  • slcopca SLCOPCA Performs Coupled PCA Learning
  • slpca SLPCA Learns a PCA model from training samples
  • slscatter SLSCATTER Compute the scatter matrix

SOURCE CODE ^

0001 function v = slmean(M, w, hasbeenchecked)
0002 %SLMEAN Compute the mean vector of samples
0003 %
0004 % $ Syntax $
0005 %   - v = slmean(M)
0006 %   - v = slmean(M, w)
0007 %   - v = slmean(M, w, true)
0008 %
0009 % $ Arguments $
0010 %   - M:        the matrix of sample vectors stored as columns
0011 %   - w:        the weights of the vectors
0012 %   - v:        the computed mean vector
0013 %
0014 % $ Description $
0015 %   - v = slmean(M) computes the mean vector of column vectors in M.
0016 %
0017 %   - v = slmean(M, w) computes the weighted mean vector of column vectors
0018 %     in M. w is the weights of the samples. if w is empty, the normal
0019 %     mean vector would be computed.
0020 %
0021 %   - v = slmean(M, w, true) indicates that the size consistency has been
0022 %     checked by invoker. Then in this function, it will not be checked
0023 %     again. This syntax is designed for the sake of efficiency.
0024 %
0025 % $ Remarks $
0026 %   - M should be a 2D matrix (d x n), then w should be a 1 x n row vector,
0027 %     v would be a d x 1 column vector.
0028 %
0029 % $ History $
0030 %   - Created by Dahua Lin on Apr 22nd, 2006
0031 %   - Modified by Dahua Lin on Sep 10th, 2006
0032 %       - replace slmul by slmulvec to increase efficiency
0033 %
0034 
0035 %% parse and verify input arguments
0036 
0037 if nargin < 3 || ~hasbeenchecked
0038 
0039     if ndims(M) ~= 2
0040         error('sltoolbox:invaliddims', 'M should be a 2D matrix');
0041     end
0042 
0043     n = size(M, 2); % number of samples
0044     if nargin < 2 || isempty(w)
0045         is_weighted = false;
0046     else
0047         is_weighted = true;
0048 
0049         % check size consistency
0050         [wd1, wd2] = size(w);
0051         if ndims(w) ~= 2 || wd1 ~= 1 || wd2 ~= n;
0052             error('sltoolbox:sizmismatch', ...
0053                 'w is not a valid row vector consistent with M');
0054         end    
0055     end
0056     
0057 else  
0058     n = size(M, 2); % number of samples
0059     is_weighted = ~isempty(w);
0060 end
0061 
0062 
0063 %% compute
0064 
0065 if ~is_weighted    
0066     v = sum(M, 2) / n;    
0067 else
0068     
0069     % normalize the weights
0070     w = w / sum(w);    
0071     v = sum(slmulvec(M, w, 2), 2);    
0072 end
0073

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

Contact us at files@mathworks.com