1
0
Fork 0

Implement PCA

master
neingeist 10 years ago
parent 5f3f65c69c
commit 2b98bd80f0

@ -1,31 +1,29 @@
function [U, S] = pca(X) function [U, S] = pca(X)
%PCA Run principal component analysis on the dataset X %PCA Run principal component analysis on the dataset X
% [U, S, X] = pca(X) computes eigenvectors of the covariance matrix of X % [U, S, X] = pca(X) computes eigenvectors of the covariance matrix of X
% Returns the eigenvectors U, the eigenvalues (on diagonal) in S % Returns the eigenvectors U, the eigenvalues (on diagonal) in S
% %
% Useful values % Useful values
[m, n] = size(X); [m, n] = size(X);
% You need to return the following variables correctly. % You need to return the following variables correctly.
U = zeros(n); U = zeros(n);
S = zeros(n); S = zeros(n);
% ====================== YOUR CODE HERE ====================== % ====================== YOUR CODE HERE ======================
% Instructions: You should first compute the covariance matrix. Then, you % Instructions: You should first compute the covariance matrix. Then, you
% should use the "svd" function to compute the eigenvectors % should use the "svd" function to compute the eigenvectors
% and eigenvalues of the covariance matrix. % and eigenvalues of the covariance matrix.
% %
% Note: When computing the covariance matrix, remember to divide by m (the % Note: When computing the covariance matrix, remember to divide by m (the
% number of examples). % number of examples).
% %
Sigma = 1/m * X'*X; % covariance matrix
[U, S, V] = svd(Sigma);
% =========================================================================
% ========================================================================= end
end