%% Sample script for the "author" data % Some cleaning commands: close all; %Close all open figures clear %Clear the memory clc %Clear the command window % Download the data from the class website, so it can be loaded here: load author; Y1=double(Y1); %Changes the data from "integers" to "floating point" %The data was saved as integers to save on memory. % These are the constants defining the matrix Y1 NumPhotos=109; %There are 109 frames, or photos, in this data set. pm=120; pn=160; % Each photo is pm x pn (in this case, 120 x 160). % We can visualize the "movie" figure(1) for j=1:NumPhotos A=reshape(Y1(:,j),pm,pn); imagesc(A); colormap(gray); axis equal; axis off; pause(0.1) end % Now that we've all had a good laugh, let's continue. %% Mean subtraction Ymean=mean(Y1,2); figure(2) imagesc(reshape(Ymean,pm,pn)); colormap(gray); axis equal; axis off; title('The Mean'); %% The SVD % We'll mean subtract, but keep the original data intact. We'll call put % the mean subtracted data in matrix X. X=Y1-repmat(Ymean,1,NumPhotos); [U,S,V]=svd(X,'econ'); %% Visualize the basis vectors- Here are 4 of them figure(3) for j=1:4 subplot(2,2,j) imagesc(reshape(U(:,j),pm,pn)); colormap(gray); axis equal; axis off end title('The first four basis vectors'); %% Projection to R^2 (Low dimensional representation) Xcoords=U(:,1:2)'*X; %The matrix Xcoords is now 2 x 109. figure(4) plot(Xcoords(1,:),Xcoords(2,:),'.'); title('Best two dimensional basis'); %% The Best five dimensional reconstruction as a "movie" % Uncomment and run if you want to see the movie %figure(5) %Xcoords=U(:,1:5)'*X; %Recon=U(:,1:5)*Xcoords; %Note: This is UU^Tx, or the projection. % for j=1:NumPhotos % A=reshape(Recon(:,j)+Ymean,pm,pn); % imagesc(A); colormap(gray); axis equal; axis off; % pause(0.1); %Make this longer or shorter to see each frame. % end