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