0001 function [GC, spectrum, LT] = sllocalcoordalign(GM, LC, dg)
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
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045 if nargin < 2
0046 raise_lackinput('sllocalcoordalign', 2);
0047 end
0048
0049 gi = slgraphinfo(GM, {'adjmat', 'square', 'numeric'});
0050 n = gi.n;
0051
0052 if ~isnumeric(LC) || ndims(LC) ~= 2
0053 error('sltoolbox:invalidarg', ...
0054 'LC should be a 2D numeric matrix');
0055 end
0056 [dl, ny] = size(LC);
0057 if ny ~= nnz(GM)
0058 error('sltoolbox:invalidarg', ...
0059 'The number of samples in LC does not match the non-zero entries in GM');
0060 end
0061
0062 if nargin < 3
0063 dg = dl;
0064 end
0065
0066 if nargout >= 3
0067 learnLT = true;
0068 else
0069 learnLT = false;
0070 end
0071
0072
0073
0074
0075
0076 B = prepareBmat(GM, n);
0077 if learnLT
0078 LRs = cell(n, 1);
0079 end
0080
0081
0082
0083 for i = 1 : n
0084
0085
0086 cinds = find(GM(:,i));
0087 curlc = LC(:, GM(cinds, i));
0088
0089
0090 cn = size(curlc, 2);
0091 [cu, cs, cv] = svd(curlc, 0);
0092
0093
0094 cs = diag(cs);
0095 rk = max(sum(cs > eps(cs(1)) * cn), 1);
0096
0097
0098 if rk < cn
0099 cv = cv(:, 1:rk);
0100 W = eye(cn) - cv * cv';
0101 vm = sum(W, 1) / cn;
0102 M = sladdrowcols(W, -vm, -vm') + sum(vm) / cn;
0103 B(cinds, cinds) = B(cinds, cinds) + M;
0104 end
0105
0106
0107
0108 if learnLT
0109 cLTR = compute_pinv(cu(:, 1:rk), cs(1:rk), cv);
0110 LRs{i} = sladdvec(cLTR, -sum(cLTR, 1)/cn);
0111 end
0112
0113 end
0114
0115
0116 B = postprocessBmat(B);
0117
0118
0119 [spectrum, GC] = slsymeig(B, dg+1, 'ascend');
0120 spectrum = spectrum(2:dg+1);
0121 GC = GC(:, 2:dg+1)';
0122
0123
0124
0125 LT = zeros(dl, dg, n);
0126
0127 if learnLT
0128 for i = 1 : n
0129 cLR = LRs{i};
0130 cGC = GC(:, GM(:,i) ~= 0);
0131 cLT = cGC * cLR;
0132 LT(:,:,i) = cLT;
0133 end
0134 end
0135
0136
0137
0138
0139
0140
0141 function B = prepareBmat(GM, n)
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152
0153
0154
0155 nnzb = 0;
0156 for i = 1 : n
0157 cnnb = nnz(GM(:,i));
0158 nnzb = nnzb + cnnb * cnnb;
0159 end
0160 nnzb = min(nnzb, n * n);
0161
0162
0163 if n * n > nnzb * 20
0164 Z = sparse(1, 1, false, n, n, nnzb);
0165 else
0166 Z = false(n, n);
0167 end
0168
0169
0170 for i = 1 : n
0171 cinds = find(GM(:,i));
0172 Z(cinds, cinds) = 1;
0173 end
0174
0175
0176 nnzb = nnz(Z);
0177
0178
0179 if n * n > nnzb * 4
0180 B = zeros(n, n);
0181 else
0182 [I, J] = find(Z);
0183 B = sparse(I, J, 1, n, n);
0184 end
0185
0186
0187 function B = postprocessBmat(B)
0188
0189 if issparse(B)
0190 B = spfun(@(x) x - 1, B);
0191 end
0192
0193
0194 function R = compute_pinv(u, s, v)
0195
0196 R = v * diag(1 ./ s) * u';
0197
0198
0199
0200
0201
0202
0203
0204
0205
0206
0207
0208
0209
0210
0211
0212
0213
0214
0215
0216
0217