n = 1000; ds = [0:5]; for k = 1:length(ds) d = ds(k); % generate data: x = randn(n, 1); y = randn(n, 1); z = randn(n, 1); y(1:n/2) = y(1:n/2) - d; y(1+n/2:end) = y(1+n/2:end) + d; z(1:n/2) = z(1:n/2) - d; z(1+n/2:end) = z(1+n/2:end) + d; % scatter plot of data: subplot(2, 2, 1) scatter3(x, y, z, 'filled', 'MarkerEdgeColor', 'white') title(sprintf('d=%g', d)) xlabel('x') ylabel('y') zlabel('z') % histogram of data: subplot(2, 2, 3) hist(x, 20) xlabel('x') % pca: cv = cov([x y z]); [ev en] = eig(cv); [en inx] = sort(diag(en), 'descend'); nc = [x y z]*ev(:,inx); % scatter in new coordinates: subplot(2, 2, 2) scatter3(nc(:, 1), nc(:, 2), nc(:, 3), 'filled', 'MarkerEdgeColor', 'white') title(sprintf('d=%g', d)) xlabel('pca 1') ylabel('pca 2') zlabel('pca 3') % histogram of data: subplot(2, 2, 4) hist(nc(:, 1), 20) xlabel('pca 1') if d == 2 savefigpdf(gcf, 'pca3d-2.pdf', 15, 15); end pause(1.0) end