Matlab code example for PCA

1,552 views
Skip to first unread message

Hiho

unread,
Oct 28, 2007, 11:39:03 AM10/28/07
to MathTools
Most useful Matlab command list:
Visualization:
plot, scatter, scatter3, quiver, quiver3, image, hist
Data Manipulation:
ones, eye, rand, sort, find, svd, diag, eigs

There is an example of PCA analysis in the following; it will show you
the change of the 3D data distribution after you making the PCA
analysis on them. The whole algorithm is given below:
x=rand([3 2])*rand([2 100]);
scatter3(x(1,:), x(2,:), x(3,:))
x0=sum(x, 2)/size(x,2);
hold on;
scatter3(x0(1,:), x0(2,:), x0(3,:), 'r')
xv=x-x0*ones([1 100]);
figure;scatter3(xv(1,:), xv(2,:),xv(3,:))
[u,s,v]=svd(xv);
evalue=diag(s(1:3,1:3));
figure;plot(evalue)
x02=x0*ones([1 2]);
figure; scatter3(x(1,:), x(2,:), x(3,:))
hold on;
scatter3(x0(1,:), x0(2,:), x0(3,:), 'r')
hold on
quiver3(x02(1,:), x02(2,:), x02(3,:), u(1,1:2), u(2,1:2), u(3, 1:2))
pc=u(:, 1:2);
xp=pc'*xv;
figure;scatter(xp(1,:),xp(2,:))
col=sum(x,1);
figure;scatter(xp(1,:),xp(2,:), col)
figure;scatter(xp(1,:),xp(2,:), 20,col)
figure;scatter3(x(1,:),x(2,:),x(3,:), 20, col)

Reply all
Reply to author
Forward
0 new messages