0001 function S = sllabeledsum(X, labels, labelset, w)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
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
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