source: proiecte/PPPP/ica/pcabigFn.m @ 94

Last change on this file since 94 was 94, checked in by (none), 14 years ago

PPPP - ica serial implementation

File size: 1.7 KB
Line 
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
27function [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
34mb=mean(B');
35B=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
43eigvalvec = max(D);
44[seigvals, index] = sort(eigvalvec); % sort goes low to high
45Vsort = V(:,[fliplr(index)]);
46
47%********Reconstruct
48U = B*Vsort;  % Cols of U are Bvi. (N-dim Eigvecs)
49
50%********Give eigvecs unit length.  Improves classification.
51length = sqrt (sum (U.^2));
52U = U ./ (ones(N,1) * length);
53
54R = B'*U;  % Rows of R are pcarep of each image.
55E = fliplr(seigvals);
Note: See TracBrowser for help on using the repository browser.