-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnipals_decomp.m
executable file
·56 lines (37 loc) · 1.07 KB
/
nipals_decomp.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
function RESULTS = nipals_decomp(Z,rows, cols, a, it, tol, LargeX)
% EIGENVALUES
RESULTS.Eigenvalues = svd(Z,0).^2;
RESULTS.Variance = RESULTS.Eigenvalues./sum(RESULTS.Eigenvalues)*100;
% INIZIALIZATION
RESULTS.Scores = zeros(rows, a);
RESULTS.Loadings = zeros(cols, a);
nr = 0;
% NIPALS DECOMPOSITION
for h=1:a
th = Z(:,LargeX);
ende = false;
while(~ende)
nr = nr+1;
th_old = th;
ph = Z'*th/(th'*th);
ph = normc(ph);
th = Z*ph/(ph'*ph);
prec = (th-th_old)'*(th-th_old);
if prec <= tol^2;
ende = true;
elseif it <=nr
ende = true;
disp('Iterarion stops without convergence!')
end
end
Z = Z-th*ph';
RESULTS.Scores(:, h) = th;
RESULTS.Loadings(:,h) = ph;
nr = 0;
end
% RESIDUAL MATRIX
RESULTS.Residual_Matrix = Z;
% STATISTICS ON RESIDUAL MATRIX
RESULTS.Residual_Matrix_Stat = matrix_stat(Z);
RESULTS.Scores_Scaled = scaledata(RESULTS.Scores,-1,1);
RESULTS.Loadings_Scaled = scaledata(RESULTS.Loadings,-1,1);