Is this the right way to obtain first PC?

4 次查看(过去 30 天)
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?

回答(1 个)

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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Dimensionality Reduction and Feature Extraction 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by