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