title: Is my Matlab code correct for applying PCA to data?
body: <p>I have following code for calculating PCA in Matlab:</p>
  
  <pre><code>train_out = train';
  test_out = test';
  % subtract off the mean for each dimension
  mn = mean(train_out,2);
  train_out = train_out - repmat(mn,1,train_size);
  test_out = test_out - repmat(mn,1,test_size);
  % calculate the covariance matrix
  covariance = 1 / (train_size-1) * train_out * train_out';
  % find the eigenvectors and eigenvalues
  [PC, V] = eig(covariance);
  % extract diagonal of matrix as vector
  V = diag(V);
  % sort the variances in decreasing order
  [junk, rindices] = sort(-1*V);
  V = V(rindices);
  PC = PC(:,rindices);
  % project the original data set
  out = PC' * train_out;
  train_out = out';
  out = PC' * test_out;
  test_out = out';
  </code></pre>
  
  <p>Train and test matrix have observations in rows and feature variables in columns. When I perform classification on original data (without PCA) I get much better results than with PCA, even when I keep all dimensions. When I tried doing PCA directly on the whole dataset (train + test) I noticed correlation between these new principal components and previous ones are either near 1 or near -1 which I find strange. I am probably doing something wrong but just can't figure it out.</p>