资源简介
网上搜集到的几份PCA代码,全部为MATLAB语言,PCA.m为一个独立调用的韩式,完成PCA功能。pca2D.m完成通过PCA完成2D数据的分割。其余的比较复杂,有需要的可以研究一下。
代码片段和文件信息
function varargout = pca(X N method)
%PCA PRINCIPLE COMPONENTS ANALYSIS
%
% Performing principal components analysis on the N1-by-N2 real-valued
% data matrix X where N1 and N2 are the number of features (N1 variables)
% and observations (N2 samples) respectively.
%
% P = pca(XNmethod) returns the N-by-N1 matrix P of basis vectors one
% basis vector per row in order of decreasing corresponding eigenvalues
% i.e. P*X retains the first N principle components. If N is not
% specified all components (N=N1) are kept.
%
% Two methods are available: ‘eig‘ (default) and ‘svd‘ which solve the
% problem by eigenvalue decomposition and singular value decomposition
% respectively. ‘svd‘ is running in ‘economy‘ mode. If there are more
% variables than samples (N1>N2) ‘svd‘ is recommended for this code.
%
% [P D] = pca(XNmethod) also returns all eigenvalues (normalized) in D
% in descending order.
%
% [P D Y] = pca(XNmethod) further returns the N-by-N2 matrix Y = P*X
% each column of whom is the projection of the corresponding observation
% from X onto the basis vectors contained in P.
%
% Siqing Wu <6sw21@queensu.ca>
% Version: 1.1 Date: 2008-07-30
error(nargchk(13nargin)) % check the number of arguments
error(nargoutchk(03nargout))
[nr nc] = size(X);
if nargin<3
method = ‘eig‘; % default method
end
if nargin<2
N = nr; % keep all components
elseif N < 1 || N > nr || round(N)~=N
fprintf(‘Input N=%g is not valid; all components will be retained.\n‘ N)
N = nr;
end
X = X-repmat(mean(X2)[1 nc]); % center data
switch method
case ‘eig‘
C = X*X.‘;
% should be C/(nc-1) for unbiased estimate of the covariance matix
% but won‘t affect P.
[ED] = eig(C); % D lists eigenvalues from small to large
% rearrange D and E
D = diag(D);
D = D(end:-1:1)/max(D);
E = E(:end:-1:1)‘;
P = E(1:N:);
case ‘svd‘
% Instead of solving X*X‘ = E*D*E‘ solve X = U*Sigma*V‘
% X*X‘ = U*Sigma*V‘*V*Sigma*U‘ = U*Sigma^2*U‘ -> P = U‘;
[Usigma] = svd(X‘econ‘); % “economy size“ decomposition
if N > nc
fprintf(‘SVD -> N=%d is used.\n‘ nc)
N = nc;
end
D = diag(sigma).^2;
D = D/max(D);
P = U(:1:N)‘;
otherwise
error(‘Undefined method!‘)
end
fprintf(‘Top %d components are retained; cumulative eigenvalue contribution is %1.2f\n‘ N sum(D(1:N))/sum(D))
switch nargout
case {01}
varargout = {P};
case {2}
varargout = {P D};
case {3}
Y = P*X;
varargout = {P D Y};
end
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 2748 2012-02-21 11:12 pca.m
文件 1400 2012-02-21 10:57 pca2D.m
文件 5211 2012-02-21 10:38 PCAexample.m
文件 12236 2009-06-11 16:03 PCA_Nicolas\PCA_Nicolas.m
文件 218 2009-06-09 22:24 PCA_Nicolas\dim3_data.mat
文件 240 2009-06-09 14:05 PCA_Nicolas\dim_data.mat
相关资源
- 基于Matlab的ARMA模型时间序列分析法仿
- matlab gui 图像灰度级更改和轮廓提取
- 全面详解LTE:MATLAB建模、仿真与实现
-
薛定宇 陈阳泉基于 MATLAB+Simuli
nk 的 - [数字滤波器的MATLAB与FPGA实现——Al
- 先进PID控制MATLAB仿真 PDF 高清文字版
- 电力电子技术课程设计 48W BUCK/BOOST电
- 滑模变结构控制MATLAB仿真 第3版 基本
- Matlab+cpp三维矩量法MoM通用计算程序
- 《MATLAB图像与视频处理实用案例详解
- 智能预测控制及其matlab实现207273
- FIR滤波器的MATLAB及FPGA实现代码
- insar图像配准及滤波
- 数字滤波器的MATLAB与FPGA实现:ALTERA
- MATLAB数字信号处理85个实用案例精讲入
- BLS宽度学习matlab代码.zip
- 放大转发AF模式的matlab仿真
- 单通道盲源分离SSA-ICA算法Matlab代码
- MIMO OFDM matlab仿真程序还有论文-MIMO
- 数字通信同步技术的MATLAB与FPGA实现完
- 数字信号处理MATLAB版高清版.pdf
- MATLAB R2016a完全自学一本通
- MATLAB在数学建模中的应用上下_源程序
- 先进PID控制MATLAB仿真.pdf
- MATLAB在数学建模中的应用卓金武
- 滑模变结构控制MATLAB仿真第3版+基本理
- MATLAB神经网络43个案例分析.pdf+源代码
- 系统辨识与自适应控制MATLAB仿真修订
- MATLAB数据探索性分析原书第二版
- Matlab提取图像的形状、纹理、颜色特
评论
共有 条评论