# Is this the right way to obtain first PC?

4 visualizzazioni (ultimi 30 giorni)
sidra il 17 Nov 2013
Commentato: Walter Roberson il 22 Dic 2015
I have used the following standard code for PCA.
% code
%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
end
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 ;
I1=im2double(I);
[signals,pc,V]=pca1(I1);
disp('signals - MxN matrix of projected data ');
disp(signals);
subplot(2,1,1);
plot(signals);
disp('PC - each column is a PC');
disp(pc);
subplot(2,1,2);
plot(pc);
fpc=pc(1);
disp('First principal component');
disp(fpc)
disp('V - Mx1 matrix of variances');
disp(V);
Does 'fpc' return the first principal component? Is what i have done ok?
##### 0 CommentiMostra -1 commenti meno recentiNascondi -1 commenti meno recenti

Accedi per commentare.

### Risposte (1)

sidra il 10 Dic 2013
Is this the right way?
##### 2 CommentiMostra 1 commento meno recenteNascondi 1 commento meno recente
Walter Roberson il 22 Dic 2015
rindices was returned by the call to sort() and is the indices of the values in descending order. V(rindices) is V re-ordered into that descending order, and PC(:,rindices) is PC with the columns reordered in that same order.

Accedi per commentare.

### Categorie

Scopri di più su Dimensionality Reduction and Feature Extraction in Help Center e File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!