pca.m 888 B

12345678910111213141516171819202122232425262728293031
  1. function [U, S] = pca(X)
  2. %PCA Run principal component analysis on the dataset X
  3. % [U, S, X] = pca(X) computes eigenvectors of the covariance matrix of X
  4. % Returns the eigenvectors U, the eigenvalues (on diagonal) in S
  5. %
  6. % Useful values
  7. [m, n] = size(X);
  8. % You need to return the following variables correctly.
  9. U = zeros(n);
  10. S = zeros(n);
  11. % ====================== YOUR CODE HERE ======================
  12. % Instructions: You should first compute the covariance matrix. Then, you
  13. % should use the "svd" function to compute the eigenvectors
  14. % and eigenvalues of the covariance matrix.
  15. %
  16. % Note: When computing the covariance matrix, remember to divide by m (the
  17. % number of examples).
  18. %
  19. sigma = 1 / m * X' * X;
  20. [U, S, V] = svd(sigma);
  21. % =========================================================================
  22. end