function [pc_vec, pc_coeff] = prcomp_naive(X, npc)
% PRCOMP_NAIVE: uses the eigendecomposition of the covariance matrix
% to compute the principal components.
%
% [pc_vec, pc_coeff] = prcomp_naive(X, npc)

Sg = cov(X);
if nargin < 2
    [pc_vec, L] = eig(Sg); 
else
    % use eigs() for only a few eigenvectors:
    npc = min(npc, min(size(X)));
    [pc_vec, L] = eigs(Sg, npc);
end

pc_coeff = diag(L);

% make sure the principal vectors and the corresponding coefficients
% are odered decreasingly:
[pc_coeff, k] = sort(pc_coeff, 'descend');
pc_vec = pc_vec(:,k);

return

% Example:
% [pv,pc] = prcomp_naive(X2, 2);
% Z2 = X2 * pv;
% hold on
% plot(Z2(:,1),Z2(:,2),'o')

% [pv,pc] = prcomp_naive(X4, 2);
% Z4 = X4 * pv;
% hold on
% plot(Z4(:,1),Z4(:,2),'o')
% % prostate cancer samples:
% plot(Z4(Y4==7,1),Z4(Y4==7,2),'ro', 'LineWidth', 2) 