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

slmeans

PURPOSE ^

SLMEANS Compute the mean vectors

SYNOPSIS ^

function varargout = slmeans(X, w, nums)

DESCRIPTION ^

SLMEANS Compute the mean vectors

 $ Syntax $
   - v = slmeans(X)
   - v = slmeans(X, w)
   - V = slmeans(X, [], nums)
   - V = slmeans(X, w, nums)
   - [V, v] = slmeans(X, [], nums)
   - [V, v] = slmeans(X, w, nums)

 $ Arguments $
   - X:            the sample matrix with each column representing a sample
   - w:            the sample weights
   - nums:         the number of samples in each group
   - v:            the overall mean vector
   - V:            the matrix of mean vectors for each group

 $ Description $
   - v = slmeans(X) computes the mean vector of X

   - v = slmeans(X, w) computes the weighted mean vector of X, with weights
     specified by w.

   - V = slmeans(X, [], nums) computes the mean vectors for groups, 
     each group of vectors are gathered together, and the number
     of vectors in each collection is specified in nums

   - V = slmeans(X, w, nums) computes the weighted mean vectors for 
     groups of vectors.

   - [V, v] = slmeans(X, w, nums) computes the mean vectors for groups
     of vectors and the overall mean vector v.

   - [V, v] = slmeans(X, w, nums) computes the weighted mean vectors for
     groups of vectors and the overall weighted mean vector v.

 $ Remarks $
   - If there are n samples in d-dimensional space, then X should be a 
     d x n matrix, w should be a 1 x n row vector. If the vectors are
     grouped in k groups, then nums should be a 1 x k row vector.
     Then v would be a d x 1 vector, V would be a d x k matrix.

 $ History $
   - Created by Dahua Lin on Dec 18th, 2005
   - Modified by Dahua Lin on Apr 22nd, 2005
       - extract the function computing mean of a group to an external
         function.
       - modify some comments

CROSS-REFERENCE INFORMATION ^

This function calls:
  • slmean SLMEAN Compute the mean vector of samples
  • slnums2bounds SLNUMS2BOUNDS Compute the index-boundaries from section sizes
This function is called by:
  • sldlda SLDLDA Performs Direct Linear Discriminant Analysis
  • slfld SLFLD Performs Fisher Linear Discriminant Analysis
  • slnlda SLNLDA Performs Nullspace-based Linear Discriminant Analysis
  • slscatter SLSCATTER Compute the scatter matrix

SOURCE CODE ^

0001 function varargout = slmeans(X, w, nums)
0002 %SLMEANS Compute the mean vectors
0003 %
0004 % $ Syntax $
0005 %   - v = slmeans(X)
0006 %   - v = slmeans(X, w)
0007 %   - V = slmeans(X, [], nums)
0008 %   - V = slmeans(X, w, nums)
0009 %   - [V, v] = slmeans(X, [], nums)
0010 %   - [V, v] = slmeans(X, w, nums)
0011 %
0012 % $ Arguments $
0013 %   - X:            the sample matrix with each column representing a sample
0014 %   - w:            the sample weights
0015 %   - nums:         the number of samples in each group
0016 %   - v:            the overall mean vector
0017 %   - V:            the matrix of mean vectors for each group
0018 %
0019 % $ Description $
0020 %   - v = slmeans(X) computes the mean vector of X
0021 %
0022 %   - v = slmeans(X, w) computes the weighted mean vector of X, with weights
0023 %     specified by w.
0024 %
0025 %   - V = slmeans(X, [], nums) computes the mean vectors for groups,
0026 %     each group of vectors are gathered together, and the number
0027 %     of vectors in each collection is specified in nums
0028 %
0029 %   - V = slmeans(X, w, nums) computes the weighted mean vectors for
0030 %     groups of vectors.
0031 %
0032 %   - [V, v] = slmeans(X, w, nums) computes the mean vectors for groups
0033 %     of vectors and the overall mean vector v.
0034 %
0035 %   - [V, v] = slmeans(X, w, nums) computes the weighted mean vectors for
0036 %     groups of vectors and the overall weighted mean vector v.
0037 %
0038 % $ Remarks $
0039 %   - If there are n samples in d-dimensional space, then X should be a
0040 %     d x n matrix, w should be a 1 x n row vector. If the vectors are
0041 %     grouped in k groups, then nums should be a 1 x k row vector.
0042 %     Then v would be a d x 1 vector, V would be a d x k matrix.
0043 %
0044 % $ History $
0045 %   - Created by Dahua Lin on Dec 18th, 2005
0046 %   - Modified by Dahua Lin on Apr 22nd, 2005
0047 %       - extract the function computing mean of a group to an external
0048 %         function.
0049 %       - modify some comments
0050 %
0051 
0052 %% parse and verify input arguments
0053 if ndims(X) ~= 2
0054     error('sltoolbox:invaliddims', 'X should be a 2D matrix');
0055 end
0056 [d, n] = size(X);
0057 
0058 % for weights
0059 if nargin < 2 || isempty(w)
0060     w = [];
0061 else
0062     if ~isequal(size(w), [1, n])
0063         error('sltoolbox:sizmismatch', ...
0064             'the weight vector should be a 1 x n row vector');
0065     end
0066 end
0067 
0068 % for groupping
0069 if nargin < 3 || isempty(nums)
0070     isgrouped = false;
0071 else
0072     isgrouped = true;
0073     if size(nums, 1) ~= 1
0074         error('sltoolbox:invalidarg', ...
0075             'the nums vector should be a row vector');
0076     end
0077     if sum(nums) ~= n
0078         error('sltoolbox:sizmismatch', ...
0079             'the nums vector does not match the total number of vectors');
0080     end
0081     [sp, ep] = slnums2bounds(nums);  % group index boundary
0082     k = length(nums);  % number of groups
0083 end
0084 
0085 %% compute
0086 if ~isgrouped
0087     v = slmean(X, w, true);
0088     varargout = {v};
0089 else
0090     V = zeros(d, k);       
0091     
0092     % compute group-wise mean
0093     if isempty(w)
0094         for i = 1 : k
0095             V(:, i) = slmean(X(:, sp(i):ep(i)), [], true);
0096         end
0097     else
0098         for i = 1 : k
0099             V(:, i) = slmean(X(:, sp(i):ep(i)), w(sp(i):ep(i)), true);
0100         end
0101     end
0102     
0103     if nargout <= 1
0104         varargout = {V};
0105     else
0106         % compute group weights
0107         if isempty(w)
0108             gw = nums;
0109         else
0110             gw = zeros(1, k);
0111             for i = 1 : k
0112                 gw(i) = sum(w(sp(i):ep(i)));
0113             end
0114         end
0115         
0116         % compute overall mean
0117         v = slmean(V, gw, true);
0118         
0119         varargout = {V, v};
0120     end
0121 end
0122 
0123 
0124

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

Contact us at files@mathworks.com