Path: blob/master/Week 8/Programming Assignment - 7/ex7/pca.m
863 views
function [U, S] = pca(X)1%PCA Run principal component analysis on the dataset X2% [U, S, X] = pca(X) computes eigenvectors of the covariance matrix of X3% Returns the eigenvectors U, the eigenvalues (on diagonal) in S4%56% Useful values7[m, n] = size(X);89% You need to return the following variables correctly.10U = zeros(n);11S = zeros(n);1213% ====================== YOUR CODE HERE ======================14% Instructions: You should first compute the covariance matrix. Then, you15% should use the "svd" function to compute the eigenvectors16% and eigenvalues of the covariance matrix.17%18% Note: When computing the covariance matrix, remember to divide by m (the19% number of examples).20%21Sigma = (1/m)*(X'*X);22[U, S, V] = svd(Sigma);23% =========================================================================2425end262728