1 | %function [U,R,E] = pcabigFn(B); |
---|
2 | %Compute PCA by calculating smaller covariance matrix and reconstructing |
---|
3 | %eigenvectors of large cov matrix by linear combinations of original data |
---|
4 | %given by the eigenvecs of the smaller cov matrix. |
---|
5 | %Data in Cols of B. Third version. |
---|
6 | % |
---|
7 | %***** justification |
---|
8 | % |
---|
9 | %B = N x P data matrix. N = dim of data (Data in Cols of B, zero mean) |
---|
10 | % P = #examples |
---|
11 | % N >> P |
---|
12 | % |
---|
13 | %Want eigenvectors ui of BB' (NxN) |
---|
14 | %Solution: |
---|
15 | %Find eigenvectors vi of B'B (PxP) |
---|
16 | %From def of eigenvector: B'Bvi = di vi ---> BB'Bvi = di Bvi |
---|
17 | %Eigenvecs of BB' are Bvi |
---|
18 | %------------------------------- |
---|
19 | %[V,D] = eig (B'B) |
---|
20 | %Eigenvecs are in cols of V. (Sorted cols) |
---|
21 | % |
---|
22 | %U = BV; Cols of U are Bvi (eigenvecs of lg cov mat.) (Gave unit length) |
---|
23 | %R = B'U; Rows of R are pcarep of each observation. |
---|
24 | %E = eigenvalues (eigenvals of small and large cov mats are equal) |
---|
25 | %***** |
---|
26 | |
---|
27 | function [U,R,E] = pcabigFn(B); |
---|
28 | |
---|
29 | %Read data into columns of B; |
---|
30 | %B = datamat'; |
---|
31 | [N,P] = size(B); |
---|
32 | |
---|
33 | %********subtract mean |
---|
34 | mb=mean(B'); |
---|
35 | B=B-(ones(P,1)*mb)'; |
---|
36 | |
---|
37 | %********Find eigenvectors vi of B'B (PxP) |
---|
38 | [V,D] = eig (1/(P-1)*(B'*B)); %scale factor gives eigvals correct |
---|
39 | %magnitude for large cov mat |
---|
40 | %(assuming sample cov) |
---|
41 | %(assuming sample cov) |
---|
42 | %********Sort eigenvectors |
---|
43 | eigvalvec = max(D); |
---|
44 | [seigvals, index] = sort(eigvalvec); % sort goes low to high |
---|
45 | Vsort = V(:,[fliplr(index)]); |
---|
46 | |
---|
47 | %********Reconstruct |
---|
48 | U = B*Vsort; % Cols of U are Bvi. (N-dim Eigvecs) |
---|
49 | |
---|
50 | %********Give eigvecs unit length. Improves classification. |
---|
51 | length = sqrt (sum (U.^2)); |
---|
52 | U = U ./ (ones(N,1) * length); |
---|
53 | |
---|
54 | R = B'*U; % Rows of R are pcarep of each image. |
---|
55 | E = fliplr(seigvals); |
---|