diff pca.m @ 3:069653867b3b

Implement PCA
author Jordi Gutiérrez Hermoso <jordigh@octave.org>
date Tue, 06 Dec 2011 03:22:07 -0500
parents ded78d0b4987
children
line wrap: on
line diff
--- a/pca.m
+++ b/pca.m
@@ -1,31 +1,9 @@
 function [U, S] = pca(X)
-%PCA Run principal component analysis on the dataset X
-%   [U, S, X] = pca(X) computes eigenvectors of the covariance matrix of X
-%   Returns the eigenvectors U, the eigenvalues (on diagonal) in S
-%
-
-% Useful values
-[m, n] = size(X);
-
-% You need to return the following variables correctly.
-U = zeros(n);
-S = zeros(n);
+  ##PCA Run principal component analysis on the dataset X
+  ##   [U, S, X] = pca(X) computes eigenvectors of the covariance matrix of X
+  ##   Returns the eigenvectors U, the eigenvalues (on diagonal) in S
+  ##
 
-% ====================== YOUR CODE HERE ======================
-% Instructions: You should first compute the covariance matrix. Then, you
-%               should use the "svd" function to compute the eigenvectors
-%               and eigenvalues of the covariance matrix. 
-%
-% Note: When computing the covariance matrix, remember to divide by m (the
-%       number of examples).
-%
+  [U, S, ~] = svd (X'*X/rows (X));
 
-
-
-
-
-
-
-% =========================================================================
-
-end
+endfunction