3 %>@brief Calculates scatter matrices from X and classes
5 %> @param X [no]x[nf] matrix
6 %> @param classes [no]x[1] matrix
7 %> @param flag_modified_s_b
if 1, S_B will be calculated in an alternative way which is
class terms will not be weighet by class sample size, which
8 %> will cause all classes to have equal importance.
9 %> @param P penalty matrix to be added to @c S_W
10 %> @return <em>[S_B, S_W]</em> Respectively
"inter-class scatter matrix" and
"within-class scatter matrix"
15 if ~exist(
'flag_modified_s_b',
'var')
16 flag_modified_s_b = 0;
24 % assumes the coefficients have been passed
30 m = mean(X); % total mean vector
32 no_classes = max(classes)+1;
38 Xi = X(classes == i-1, :);
40 m_i = mean(Xi); % vector containing the means for each column/feature.
42 Xi = Xi-repmat(m_i, size(Xi, 1), 1); % centers each column to its mean
44 n_i = size(Xi, 1); % number of observations for class i
46 scatter = Xi'*Xi; % scatter matrix. This is S_i in the reference
48 S_B = S_B+(m_i-m)'*(m_i-m); % last term is a rank-1 matrix (MATLAB's default is row vector)
50 S_B = S_B+n_i*(m_i-m)'*(m_i-m); % last term is a rank-1 matrix (MATLAB's default is row vector)
59 % I have found empirically that adding P to S_W is equivalent to adding
60 % P to X'*X in canonical correlation analysis
function calculate_scatters(in X, in classes, in flag_modified_s_b, in P)
function penalty_matrix(in nf, in dcoeff)