Do anyone know what is the algorithm that MATLAB used in its built-in function "pca"?
I have the following data set:
148.9820 55.8438 210.2150
149.3030 56.8891 208.4280
151.4400 55.8180 208.9240
146.5530 55.9677 211.5800
146.5770 57.2682 209.3680
145.5330 58.4735 207.6970
153.9680 55.8386 207.9600
143.6960 57.2371 211.1020
152.5960 57.3995 206.2770
144.1070 56.1439 212.9730
149.6670 58.8746 205.1560
142.8440 58.1240 209.7220
143.2190 59.3990 207.0410
146.3050 60.2445 204.1980
156.7100 55.9361 207.4610
141.0470 57.3240 212.4660
where the number of rows are number of observations and each observation is of dimension 3.
I want to perform principal component analysis on this data set so I wrote
P = pca(A)
where A is the above matrix. The answer I got is
0.9480 0.2104 0.2387
-0.0980 -0.5204 0.8483
-0.3027 0.8276 0.4727
However, when I use the following program:
function [evects,evals] = pca_test(dataset)
if (size(dataset,1)>size(dataset,2))
dataset = dataset';
end
N = size(dataset,2);
mm = mean(dataset,2);
dataset = dataset - mm*ones(1,N);
cc = cov(dataset',1);
[cvv,cdd] = eig(cc);
[~,ii] = sort(diag(cdd));
ii = flip(ii,1);
evects = cvv(:,ii);
cdd = diag(cdd);
evals = cdd(ii);
it gives
evects =
-0.9480 0.2104 0.2387
0.0980 -0.5204 0.8483
0.3027 0.8276 0.4727
The first column is of opposite sign to the result generated by the built-in pca. Why is there such a change?
I ask this because I think the matlab built-in pca is really slow. The pca_test above is around 3 times faster than the built-in function. But I want it to have exactly the same result as the built-in one. Can anyone help?