Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hackassin
GitHub Repository: hackassin/Coursera-Machine-Learning
Path: blob/master/Week 8/Programming Assignment - 7/ex7/pca.m
863 views
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
7
% Useful values
8
[m, n] = size(X);
9
10
% You need to return the following variables correctly.
11
U = zeros(n);
12
S = zeros(n);
13
14
% ====================== YOUR CODE HERE ======================
15
% Instructions: You should first compute the covariance matrix. Then, you
16
% should use the "svd" function to compute the eigenvectors
17
% and eigenvalues of the covariance matrix.
18
%
19
% Note: When computing the covariance matrix, remember to divide by m (the
20
% number of examples).
21
%
22
Sigma = (1/m)*(X'*X);
23
[U, S, V] = svd(Sigma);
24
% =========================================================================
25
26
end
27
28