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

slarrmean

PURPOSE ^

SLARRMEAN Computes the mean of a set of arrays

SYNOPSIS ^

function arrMean = slarrmean(data, arrsiz, n, varargin)

DESCRIPTION ^

SLARRMEAN Computes the mean of a set of arrays

 $ Syntax $
   - slarrmean(arrs, arrsiz, n, ...)
   - slarrmean(fns, arrsiz, n, ...)

 $ Arguments $
   - arrs:         the super-array consisting of all arrays
   - fns:          the file paths of all array files
   - arrsiz:       the size of each array unit. (for a column vector, it
                   is the length of the vector)
   - n:            the total number of array units

 $ Description $
   - slarrmean(arrs, arrsiz, n, ...) computes the mean of all array units
     with the size of each unit specified by arrsiz. If there are more 
     than one array unit, the size of arrs should be [arrsiz, n].

   - slarrmean(fns, arrsiz, n, ...) computes the mean of all array units
     stored in the array files given in fns. Each array file stores
     an super-array of a set of array units.

   - You can specify additional properties.
       \t      The properties of slarrmean
       \h      name       &        description
              'weights'   &  The weights of each array unit, default = []
             
 $ History $
   - Created by Dahua Lin on Jul 27th, 2006

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:
  • sl2dpcaex SL2DPCAEX Learns Extended 2D PCA on a set of matrix samples
  • slpartitionpca SLPARTITIONPCA Performs Partition-based PCA and saves the models

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function arrMean = slarrmean(data, arrsiz, n, varargin)
0002 %SLARRMEAN Computes the mean of a set of arrays
0003 %
0004 % $ Syntax $
0005 %   - slarrmean(arrs, arrsiz, n, ...)
0006 %   - slarrmean(fns, arrsiz, n, ...)
0007 %
0008 % $ Arguments $
0009 %   - arrs:         the super-array consisting of all arrays
0010 %   - fns:          the file paths of all array files
0011 %   - arrsiz:       the size of each array unit. (for a column vector, it
0012 %                   is the length of the vector)
0013 %   - n:            the total number of array units
0014 %
0015 % $ Description $
0016 %   - slarrmean(arrs, arrsiz, n, ...) computes the mean of all array units
0017 %     with the size of each unit specified by arrsiz. If there are more
0018 %     than one array unit, the size of arrs should be [arrsiz, n].
0019 %
0020 %   - slarrmean(fns, arrsiz, n, ...) computes the mean of all array units
0021 %     stored in the array files given in fns. Each array file stores
0022 %     an super-array of a set of array units.
0023 %
0024 %   - You can specify additional properties.
0025 %       \t      The properties of slarrmean
0026 %       \h      name       &        description
0027 %              'weights'   &  The weights of each array unit, default = []
0028 %
0029 % $ History $
0030 %   - Created by Dahua Lin on Jul 27th, 2006
0031 %
0032 
0033 %% parse and verify input arguments
0034 
0035 if nargin < 3
0036     raise_lackinput('slarrmean', 3);
0037 end
0038     
0039 if isnumeric(data)
0040     isdirect = true;
0041     arrs = data;
0042     arrsiz = arrsiz(:)';
0043     if ~isequal(size(arrs), [arrsiz, n])
0044         error('sltoolbox:sizmismatch', ...
0045             'The size of arrs (data) is invalid');
0046     end
0047     
0048 elseif iscell(data)
0049     isdirect = false;
0050     fns = data;
0051     arrsiz = arrsiz(:)';
0052     nfiles = numel(fns);
0053     
0054 else
0055     error('sltoolbox:invalidarg', ...
0056         'The first argument for slarrmean should be an numeric array or a cell array of file names');
0057 end
0058 
0059 opts.weights = [];
0060 
0061 opts = slparseprops(opts, varargin{:});
0062 hasweights = ~isempty(opts.weights);
0063 
0064 if (hasweights)
0065     opts.weights = opts.weights(:);
0066     if length(opts.weights) ~= n
0067         error('sltoolbox:invalidarg', ...
0068             'The length of weights is inconsistent with the number of units');
0069     end
0070 end
0071 
0072 
0073 %% Main skeleton
0074 
0075 if isdirect
0076     arrMean = compute_array_sum(arrs, arrsiz, n, opts.weights);
0077 else
0078     arrMean = zeros(arrsiz);
0079     c = 0;          
0080     for i = 1 : nfiles
0081         curarrs = slreadarray(fns{i});
0082         curn = size(curarrs, length(arrsiz) + 1);
0083         if ~hasweights
0084             arrMean = arrMean + compute_array_sum(curarrs, arrsiz, curn, []);
0085         else
0086             arrMean = arrMean + compute_array_sum(curarrs, arrsiz, curn, opts.weights(c+1:c+curn));
0087         end
0088         c = c + curn;
0089         
0090         if c > n
0091             error('sltoolbox:sizmismatch', ...
0092                 'The total number of units in the set of array files is not n');
0093         end
0094     end
0095     
0096     if c ~= n
0097         error('sltoolbox:sizmismatch', ...
0098             'The total number of units in the set of array files is not n');
0099     end
0100     
0101 end
0102 
0103 if ~hasweights
0104     arrMean = arrMean / n;
0105 else
0106     arrMean = arrMean / sum(opts.weights);
0107 end
0108 
0109 
0110 
0111 %% Compute function
0112 
0113 function S = compute_array_sum(arrs, arrsiz, n, w)
0114 
0115 if ~isequal(size(arrs), [arrsiz, n])
0116     error('sltoolbox:sizmismatch', ...
0117         'The size of array is not consistent as specified');
0118 end
0119 
0120 d = length(arrsiz);
0121 if isempty(w)
0122     S = sum(arrs, d+1);
0123 else
0124     S = reshape(arrs, [prod(arrsiz), n]) * w;
0125     if d == 1
0126         S = reshape(S, [arrsiz, 1]);
0127     else
0128         S = reshape(S, arrsiz);
0129     end
0130 end
0131 
0132 
0133 
0134 
0135 
0136 
0137 
0138 
0139 
0140

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

Contact us at files@mathworks.com