PCA1: Perform PCA using covariance. Usage: [signals, PC, V] = pca1(data) data (in) - MxN matrix of input data (M dimensions, N trials) signals - MxN matrix of projected data PC - each column is a principal component V - Mx1 matrix of variances data (out) - mean subtracted data.
0001 function [signals, PC, V, data] = pca1(data) 0002 % PCA1: Perform PCA using covariance. 0003 % 0004 % Usage: 0005 % [signals, PC, V] = pca1(data) 0006 % 0007 % data (in) - MxN matrix of input data 0008 % (M dimensions, N trials) 0009 % signals - MxN matrix of projected data 0010 % PC - each column is a principal component 0011 % V - Mx1 matrix of variances 0012 % data (out) - mean subtracted data. 0013 0014 % 0015 % From Jon Schlens PCA tutorial. 0016 0017 [M, N] = size(data); 0018 0019 % subtract off the mean for each dimension 0020 mn = mean(data,2); 0021 data = data- repmat(mn,1,N); 0022 0023 % calculate the covariance matrix 0024 covariance = 1/(N-1)*data*data'; 0025 0026 if (isnan(covariance)) 0027 covariance(:,:) = 0 0028 end 0029 0030 % find the eigenvectors and eigenvalues 0031 [PC, V] = eig(covariance); 0032 0033 % extract diagonal of matrix as vector 0034 V = diag(V); 0035 0036 % sort the variances in decreasing order 0037 [junk, rindices] = sort(-1*V); 0038 V = V(rindices); 0039 PC = PC(:, rindices); 0040 0041 % project the original data set 0042 signals = PC'*data;