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?
0 个评论
回答(1 个)
sidra
2013-12-10
2 个评论
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.
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!