Daniel@0: function [PCcoeff, PCvec] = pca(data, N) Daniel@0: %PCA Principal Components Analysis Daniel@0: % Daniel@0: % Description Daniel@0: % PCCOEFF = PCA(DATA) computes the eigenvalues of the covariance Daniel@0: % matrix of the dataset DATA and returns them as PCCOEFF. These Daniel@0: % coefficients give the variance of DATA along the corresponding Daniel@0: % principal components. Daniel@0: % Daniel@0: % PCCOEFF = PCA(DATA, N) returns the largest N eigenvalues. Daniel@0: % Daniel@0: % [PCCOEFF, PCVEC] = PCA(DATA) returns the principal components as well Daniel@0: % as the coefficients. This is considerably more computationally Daniel@0: % demanding than just computing the eigenvalues. Daniel@0: % Daniel@0: % See also Daniel@0: % EIGDEC, GTMINIT, PPCA Daniel@0: % Daniel@0: Daniel@0: % Copyright (c) Ian T Nabney (1996-2001) Daniel@0: Daniel@0: if nargin == 1 Daniel@0: N = size(data, 2); Daniel@0: end Daniel@0: Daniel@0: if nargout == 1 Daniel@0: evals_only = logical(1); Daniel@0: else Daniel@0: evals_only = logical(0); Daniel@0: end Daniel@0: Daniel@0: if N ~= round(N) | N < 1 | N > size(data, 2) Daniel@0: error('Number of PCs must be integer, >0, < dim'); Daniel@0: end Daniel@0: Daniel@0: % Find the sorted eigenvalues of the data covariance matrix Daniel@0: if evals_only Daniel@0: PCcoeff = eigdec(cov(data), N); Daniel@0: else Daniel@0: [PCcoeff, PCvec] = eigdec(cov(data), N); Daniel@0: end Daniel@0: