-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmbPCA.m
28 lines (24 loc) · 940 Bytes
/
mbPCA.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
function [pcDec,eigPer] = mbPCA(data)
%% Michael Bellato: Principal Components Analysis
%
% prcomp = mbPCA(data)
%
% Inputs -
% data: input data
%
% Outputs -
% prcomp: Principal Components of data in descending order
%
% Principal components are converted to Percentage of variance
%
%%
datalth = size(data,2);
datawdt = size(data,1);
dataMu = squeeze(mean(data,3)); % compute mean
dataSub = bsxfun(@minus,dataMu,mean(dataMu,2)); % subtract mean
CoVarMat = (dataSub*dataSub')./(datalth-1); % compute covariance matrix
[pc,eigvals] = eig(CoVarMat); % compute eigenvalues
seleigval = diag(eigvals); % select eigenvalues
pcDec = pc(:,end:-1:1); % PCs in descending order
eigDec = seleigval(end:-1:1); % Eigenvalues in dec order
eigPer = 100*eigDec./sum(eigDec); % Percentage of variance