title: Principal Components calculated using different functions in Matlab
body: <p>I am trying to understand principal component analysis in Matlab,</p>
  
  <p>There seems to be at least 3 different functions that do it.</p>
  
  <p>I have some questions re the code below:</p>
  
  <ol>
  <li><p>Am I creating approximate <code>x</code> values using only one eigenvector (the one corresponding to the largest eigenvalue) correctly? I think so??</p></li>
  <li><p>Why are <code>PC</code> and <code>V</code> which are both meant to be the loadings for (x'x) presented differently? The column order is reversed because eig does not order the eigenvalues with the largest value first but why are they the negative of each other?</p></li>
  <li><p>Why are the <code>eig</code> values not in ordered with the eigenvector corresponding to the largest eigenvalue in the first column?</p></li>
  <li><p>Using the code below I get back to the input matrix <code>x</code> when using <code>svd</code> and <code>eig</code>, but the results from princomp seem to be totally different? What so I have to do to make princomp match the other two functions?</p></li>
  </ol>
  
  <p>Code:</p>
  
  <pre><code>x=[1 2;3 4;5 6;7 8 ]
  
  econFlag=0;
  
  [U,sigma,V] = svd(x,econFlag);%[U,sigma,coeff] = svd(z,econFlag);
  
  U1=U(:,1);
  V1=V(:,1);
  sigma_partial=sigma(1,1);
  
  score1=U*sigma;
  test1=score1*V';
  
  score_partial=U1*sigma_partial;
  test1_partial=score_partial*V1';
  
  
  
  [PC, D] = eig(x'*x)
  
   score2=x*PC;
  test2=score2*PC';
  
  PC1=PC(:,2);
  score2_partial=x*PC1;
   test2_partial=score2_partial*PC1';
  
  [o1 o2 o3]=princomp(x);
  </code></pre>