% % Problem epage 70 % Example epage 51 % % Written by: % -- % John L. Weatherwax 2005-08-14 % % email: wax@alum.mit.edu % % Please send comments and especially bug reports to the % above email address. % %----- close all; drawnow; clc; clear; rand('seed',0); randn('seed',0); n = 10; p = 5; X = rand(n,p); m1 = (X.') * X; m2 = X * (X.'); [eigvec1,eigval1] = eig( m1 ); eigval1 = sort(diag(eigval1)); [eigvec2,eigval2] = eig( m2 ); eigval2 = sort(diag(eigval2)); fprintf('displaying the non-zero eigenvalues...\n'); flipud([ eigval1(:), eigval2(end-4:end) ]) % <- note this assumes that the eigenvalues are sorted in increasing/ascending order [u,s,v] = svd( X ); % <- assuming vectors are returned in decending order % get the diagonal elemenst of s: s_diag = diag(s); fprintf( 'the square of these sigular values are given by \n' ); (s_diag.^2).' fprintf( 'the eigenvectors of X^T X (permuted) and the right singular vectors V are\n' ); disp(eigvec1(:,p:-1:1)) disp(-v) fprintf( 'they are the same appart from sign\n\n' ); % flip the ordering of the eigenvectors so that they correspond to a decreasing eigenvalue ordering: eigvec1 = fliplr(eigvec1); eigvec2 = fliplr(eigvec2); fprintf( 'the first p=%d eigenvectors of X X^T (permuted) and the left singular vectors U are\n', p ); disp(eigvec2(:,1:p)) disp(u(:,1:p)) fprintf( 'they are the same appart from sign\n\n' );