pca.m

来自「主元分析(pca)的matlab程序,运算时间短,程序简单」· M 代码 · 共 39 行

M
39
字号
%P returns the load matrix of x;
%T returns the score matrix of x;
%W is the convert matrix from X to T;

function [Th,P,W] = pca(x,maxrank)

[m,n] = size(x);
if nargin == 2, i = min([n, m, maxrank]); else i = min([n m]); end

P = [];
Th = [];
W = [];

for h=1:i % for1
   Sell = mod(floor((rand(1,1)*10000)), n);
   t0 = x(:, Sell + 1);
   while (1)
      ph = x'*t0/(t0'*t0);
      ph = ph/sqrt(ph'*ph);
      th = x*ph/(ph'*ph);
      
      if((t0-th)'*(t0-th)<0.001)
         break;
      end
      t0 = th;
   end % end of while
   %compute the load vector of x
   
   if h==1
      Th = th;
      P = ph;
   else
      Th = [Th th];
      P = [P ph];
   end
   
   x = x - th*ph';
end % end of for1

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?