필터 지우기
필터 지우기

Is this the right way to obtain first PC?

조회 수: 3 (최근 30일)
sidra
sidra 2013년 11월 17일
댓글: Walter Roberson 2015년 12월 22일
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