-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathf_pca.m
More file actions
25 lines (24 loc) · 727 Bytes
/
Copy pathf_pca.m
File metadata and controls
25 lines (24 loc) · 727 Bytes
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
function [signals,PC,V] = f_pca(data)
% PCA1: Perform PCA using covariance.
% data - MxN matrix of input data
% (M dimensions, N triats)
% signals - MxN matrix of projected data
% PC - each column is a PC
% V - Mx1 matrix of variances
[M,N] = size(data);
% subtract off the mean for each dimension
mn = mean(data,2);
data = data - repmat(mn,1,N);
% calculate the covariance matrix
covariance = 1 / (N-1) * data * data';
% find the eigenvectors and eigenvalues
[PC, V] = eig(covariance);
% extract diagonal of matrix as vector
V = diag(V);
% sort the variances in decreasing order
[junk, rindices] = sort(-1*V);
V = V(rindices);
V = cumsum(V)./sum(V);
PC = PC(:,rindices);
% project the original data set
signals = PC' * data;