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 sllabeledsum
Home > sltoolbox > core > sllabeledsum.m

sllabeledsum

PURPOSE ^

SLLABELEDSUM Sums the numbers according to labels

SYNOPSIS ^

function S = sllabeledsum(X, labels, labelset, w)

DESCRIPTION ^

SLLABELEDSUM Sums the numbers according to labels

 $ Syntax $
   - S = sllabledsum(X, labels, labelset)
   - S = sllabledsum(X, labels, labelset, w)

 $ Arguments $
   - X:            The matrix of numbers (m x n)
   - labels:       The labels of columns in X (1 x n)
   - labelset:     The set of labels used (length-c vector)
   - w:            The column weights (1 x n) (default = [])
   - S:            The sum (m x c)

 $ Description $
   - S = sllabledsum(X, labels, labelset) sums the columns in X that 
     corresponding to the same label. If labelset has c labels, then 
     S would have c columns, the i-th column sums the columns in X 
     associative the labelset(i).

   - S = sllabledsum(X, labels, labelset, w) performs a weighted sum.

 $ History $
   - Created by Dahua Lin, on Aug 31, 2006
   - Modified by Dahua Lin, on Sep 10, 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
  • raise_lackinput RAISE_LACKINPUT Raises an error indicating lack of input argument
  • sllabelinds SLLABELINDS Extract indices corresponding to specified labels
This function is called by:
  • slkmeansex SLKMEANSEX Performs Generalized K-means
  • slcountvote SLCOUNTRULE Counts the votings to make histogram

SOURCE CODE ^

0001 function S = sllabeledsum(X, labels, labelset, w)
0002 %SLLABELEDSUM Sums the numbers according to labels
0003 %
0004 % $ Syntax $
0005 %   - S = sllabledsum(X, labels, labelset)
0006 %   - S = sllabledsum(X, labels, labelset, w)
0007 %
0008 % $ Arguments $
0009 %   - X:            The matrix of numbers (m x n)
0010 %   - labels:       The labels of columns in X (1 x n)
0011 %   - labelset:     The set of labels used (length-c vector)
0012 %   - w:            The column weights (1 x n) (default = [])
0013 %   - S:            The sum (m x c)
0014 %
0015 % $ Description $
0016 %   - S = sllabledsum(X, labels, labelset) sums the columns in X that
0017 %     corresponding to the same label. If labelset has c labels, then
0018 %     S would have c columns, the i-th column sums the columns in X
0019 %     associative the labelset(i).
0020 %
0021 %   - S = sllabledsum(X, labels, labelset, w) performs a weighted sum.
0022 %
0023 % $ History $
0024 %   - Created by Dahua Lin, on Aug 31, 2006
0025 %   - Modified by Dahua Lin, on Sep 10, 2006
0026 %       - replace slmul by slmulvec to increase efficiency
0027 %
0028 
0029 %% parse and verify input
0030 
0031 if nargin < 3
0032     raise_lackinput('sllabeledsum', 3);
0033 end
0034 
0035 if ndims(X) ~= 2 || ~isnumeric(X)
0036     error('sltoolbox:invalidarg', 'X should be a numeric 2D matrix');
0037 end
0038 [m, n] = size(X);
0039 
0040 if ~isequal(size(labels), [1 n])
0041     error('sltoolbox:sizmismatch', ...
0042         'The size of labels does not match the number of X-columns');
0043 end
0044 
0045 if ~isvector(labelset)
0046     error('sltoolbox:invalidarg', 'labelset should be a vector');
0047 end
0048 c = length(labelset);
0049 
0050 if nargin < 4
0051     w = [];
0052 else
0053     if ~isempty(w)
0054         if ~isequal(size(w), [1 n])
0055             error('sltoolbox:sizmismatch', ...
0056                 'The size of w does not match the number of X-columns');
0057         end
0058     end
0059 end
0060 
0061 %% compute
0062 
0063 Inds = sllabelinds(labels, labelset);
0064 S = zeros(m, c);
0065 
0066 
0067 for i = 1 : c
0068     curinds = Inds{i};
0069     if ~isempty(curinds)
0070         curX = X(:, curinds);        
0071         if ~isempty(w)
0072             curw = w(curinds);
0073             curX = slmulvec(curX, curw, 2);
0074         end        
0075         S(:,i) = sum(curX, 2);
0076     end
0077 end
0078 
0079 
0080 
0081     
0082 
0083 
0084

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

Contact us at files@mathworks.com