Is this the right way to obtain first PC?

7 visualizzazioni (ultimi 30 giorni)
sidra
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 ;
I=imread('lena.jpg');
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?

Risposte (1)

sidra
sidra il 10 Dic 2013
Is this the right way?
  2 Commenti
Neo
Neo il 22 Dic 2015
What does V = V(rindices); PC = PC(:,rindices); mean?
Walter Roberson
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.

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by