Is this the right way to obtain first PC?
4 visualizzazioni (ultimi 30 giorni)
I have used the following standard code for PCA.
%function [signals,PC,V] = pca1(data)
% PCA1: Perform PCA using covariance.
% data - MxN matrix of input data
% (M dimensions, N trials)
% 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); PC = PC(:,rindices);
% project the original data set
signals = PC' * data; code
I now want to extract only the first principal component. The code which calls the function and (hopefully) obtains the first principal component is:
% clc,clear ;
disp('signals - MxN matrix of projected data ');
disp('PC - each column is a PC');
disp('First principal component');
disp('V - Mx1 matrix of variances');
Does 'fpc' return the first principal component? Is what i have done ok?