我使用以下代码计算PCA:
function [signals,PC,V] = pca2(data)
[M,N] = size(data);
% subtract off the mean for each dimension
mn = mean(data,2);
data = data - repmat(mn,1,N);
% construct the matrix Y
Y = data’ / sqrt(N-1);
% SVD does it all
[u,S,PC] = svd(Y);
% calculate the variances
S = diag(S);
V = S .* S;
% project the original data
signals = PC’ * data;
我希望保持主成分具有最大方差,也就是说可能是导致最大方差的前10个主成分。我该怎么做?
答案 0 :(得分:2)
function [signals,V] = pca2(data)
[M,N] = size(data);
data = reshape(data, M*N,1);
% subtract off the mean for each dimension
mn = mean(data,2);
data = bsxfun(@minus, data, mean(data,1));
% construct the matrix Y
Y = data'*data / (M*N-1);
[V D] = eigs(Y, 10); % reduce to 10 dimension
% project the original data
signals = data * V;
答案 1 :(得分:1)
我猜svds可以为你完成这项工作。
在文档中,它说:
s = svds(A,k)计算k个最大奇异值并相关联 矩阵A的奇异向量。
本质上是k个最大的特征值和特征向量。这些按特征值按降序排序。
因此,对于10个主要组件,只需使用[eigvec eigval] = svds(Y, 10);