-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmvpcasvd.m
79 lines (68 loc) · 1.93 KB
/
mvpcasvd.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
function [T, P, eigv, varpc, res] = mvpcasvd(X, maxfact,method)
%MVPCASVD -- Principal Component Analysis using Singular Value Decomposition
%
% Usage:
% [T, P, eigv, varpc, res] = mvpcasvd(X, maxfact)
%
% Inputs:
% X is the input matrix (objects (samples) in rows)
% maxfact is an optional limit on the number of factors kept
% method 'normaleq' (default) or 'direct'
%
% Outputs:
% T matrix containing the scores
% P column vector containing the loadings
% eigv eigenvalues for each calculated factor
% res residual matrix
%
% Description:
%
%
% Copying:
% MVARTOOLS, Copyright (C) 1999-2001 Rune Mathisen <[email protected]>
% MVARTOOLS comes with ABSOLUTELY NO WARRANTY; for details type
% `mvwarranty'. This is free software, and you are welcome to
% redistribute it under certain conditions; type `mvcopying' for
% details. For more information on MVARTOOLS, type 'mvreadme'.
% $Id: mvpcasvd.m,v 1.2 2001/12/04 09:42:40 rune Exp $
% find the number of variables
[m,n] = size(X);
if (nargin == 2) | (nargin == 3),
% cannot find more PC's than number of variables
k = min([n, maxfact]);
elseif (nargin == 1) | (isempty(k) == 1),
k = n;
end
if nargin < 3,
% the default method
method = 'normaleq';
end
if abs(max(mean(X))) > 1e3*eps,
warning('Input matrix is not mean centered! Please consider doing this!');
end
% 1. SVD
if strcmp(method, 'normaleq'),
XX = X'*X;
[U,S,VT] = svd(XX);
else %if strcmp(method, 'direct')
[U,S,VT] = svd(X);
end
% Extract the eigenvalues.
eigv=diag(S);
nocomp = length(eigv);
if k < nocomp,
eigv=eigv(1:k);
else
k = nocomp;
end
% Find the loading vectors.
P = VT(:,1:k);
% Calculate the scores.
T = X*P;
% VARPC is the X variance captured in each factor.
varexp = (eigv(1:k)/sum(eigv));
for a=1:k, % The accumulated variance explained for each PC
varpc(a)=sum(varexp(1:a));
end
% The residual matrix.
res = X - T*P';