pc_evectors.m
来自「用matlab编写的找两个输入量之间的主要特征向量,主要方法采用PCA经典算法.」· M 代码 · 共 125 行
M
125 行
function [Vectors,Values,Psi] = pc_evectors(A,numvecs)
%PC_EVECTORS Get the top numvecs eigenvectors of the covariance matrix
% of A, using Turk and Pentland's trick for numrows >> numcols
% Returns the eigenvectors as the colums of Vectors and a
% vector of ALL the eigenvectors in Values.
% Matthew Dailey 2000
% Check arguments
if nargin ~= 2
error('usage: pc_evectors(A,numvecs)');
end;
nexamp = size(A,2);
% Now compute the eigenvectors of the covariance matrix, using
% a little trick from Turk and Pentland 1991
% Compute the "average" vector
% mean(A) gives you a row vector containing the mean of each column of A
fprintf(1,'Computing average vector and vector differences from avg...\n');
Psi = mean(A')';
% Compute difference with average for each vector
for i = 1:nexamp
A(:,i) = A(:,i) - Psi;
end;
% Get the patternwise (nexamp x nexamp) covariance matrix
fprintf(1,'Calculating L=A''A\n');
L = A'*A;
% Get the eigenvectors (columns of Vectors) and eigenvalues (diag of Values)
fprintf(1,'Calculating eigenvectors of L...\n');
[Vectors,Values] = eig(L);
% Sort the vectors/values according to size of eigenvalue
fprintf(1,'Sorting evectors/values...\n');
[Vectors,Values] = sortem(Vectors,Values);
% Convert the eigenvectors of A'*A into eigenvectors of A*A'
fprintf(1,'Computing eigenvectors of the real covariance matrix..\n');
Vectors = A*Vectors;
% Get the eigenvalues out of the diagonal matrix and
% normalize them so the evalues are specifically for cov(A'), not A*A'.
Values = diag(Values);
Values = Values / (nexamp-1);
% Normalize Vectors to unit length, kill vectors corr. to tiny evalues
num_good = 0;
for i = 1:nexamp
Vectors(:,i) = Vectors(:,i)/norm(Vectors(:,i));
if Values(i) < 0.00001
% Set the vector to the 0 vector; set the value to 0.
Values(i) = 0;
Vectors(:,i) = zeros(size(Vectors,1),1);
else
num_good = num_good + 1;
end;
end;
if (numvecs > num_good)
fprintf(1,'Warning: numvecs is %d; only %d exist.\n',numvecs,num_good);
numvecs = num_good;
end;
Vectors = Vectors(:,1:numvecs);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5
%The function pc_evectors uses TUrk and Pentland'd trick to get the eigenvectors of A*A' from the eigenvectors
%of the A'*A. It uses the function "sorterm" to sort the eigenvectors and eigenvalues by eigenvalue. To use the function,
%just do:
[Vecs,Vals,Psi]=pc_evectors(Imgs,30);% @get top 30 pc evectors of imgs and to ecplore the eigenvalue spectrum and how much variance the first n vectors account for, try following:
Plot(Vals); % to plot the eigenvalues
Cvals=zeros(1,length(Vals)); %Allocate a vector same length as Vals
CVals(1)=Vals(1);
for i=2:length(Vals) %Accumulate the eigenvalue sum
CVals(i)=CVals(i-1)+Vals(i);
end;
CVals=Cvals/sum(Vals); %Normalize total sum to 1.0
plot(CVals); %Plot the cumulative sum
ylim([0 1]); %Set y-axis limits to be 0-1
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?