The projected data onto the principle components is returned in the score
variable, so the plot is simply:
gscatter(score(:,1), score(:,2), species, [], [], [], 'on', 'PC1', 'PC2')
title('Projected Iris data'), grid on
of course you could perform the PCA yourself using either EIG
or SVD
:
X = meas;
X = bsxfun(@minus, X, mean(X)); % zero-centered data
[~,S,V] = svd(X,0); % singular value decomposition
[S,ord] = sort(diag(S), 'descend');
pc = V(:,ord); % principle components
latent = S.^2 ./ (size(X,1)-1) % variance explained
score = X*pc; % projected data