>> % A LITERATE PROGRAM IN MATLAB, Naren Ramakrishnan
>> % create a matrix first
>> % adapted from Berry
>> [... skipping...]

a =

	0    0.5774         0    0.4472    0.7071         0    0.7071
	0    0.5774    0.5774         0         0         0         0
	0         0         0         0         0    0.7071    0.7071
	0         0         0    0.4472         0         0         0
	0    0.5774    0.5774         0         0         0         0
   0.7071         0         0    0.4472         0         0         0
	0         0         0         0    0.7071    0.7071         0
	0         0    0.5774    0.4472         0         0         0
   0.7071         0         0    0.4472         0         0         0


>> [u,s,v] = svd(a)

u = 

  Columns 1 through 7 

      0.6977    0.0932   -0.0175    0.6951    0.0000   -0.0157   -0.1441
      0.2620   -0.2968   -0.4679   -0.1969   -0.0000    0.2468    0.1570
      0.3527    0.4492    0.1015   -0.4013    0.7071    0.0067    0.0493
      0.1121   -0.1409    0.1479    0.0733   -0.0000   -0.4842    0.8402
      0.2620   -0.2968   -0.4679   -0.1969   -0.0000    0.2468    0.1570
      0.1873   -0.3744    0.5051   -0.1270    0.0000    0.2287   -0.0339
      0.3527    0.4492    0.1015   -0.4013   -0.7071    0.0067    0.0493
      0.2105   -0.3337   -0.0952   -0.2820    0.0000   -0.7340   -0.4657
      0.1873   -0.3744    0.5051   -0.1270   -0.0000    0.2287   -0.0339

  Columns 8 through 9 

           0    0.0000
     -0.6356    0.3098
      0.0000   -0.0000
     -0.0000    0.0000
      0.6356   -0.3098
     -0.3098   -0.6356
     -0.0000    0.0000
      0.0000   -0.0000
      0.3098    0.6356

s =

    1.5777         0         0         0         0         0         0
         0    1.2664         0         0         0         0         0
         0         0    1.1891         0         0         0         0
 	 0         0         0    0.7963         0         0         0
	 0         0         0         0    0.7071         0         0
	 0         0         0         0         0    0.5664         0
	 0         0         0         0         0         0    0.1968
	 0         0         0         0         0         0         0
	 0         0         0         0         0         0         0

%
% notice the descending pattern of the singular values of a
%
v =

    0.1679   -0.4181    0.6008   -0.2255         0    0.5710   -0.2433
    0.4472   -0.2282   -0.4629    0.2185   -0.0000    0.4872    0.4986
    0.2688   -0.4228   -0.5006   -0.4900    0.0000   -0.2451   -0.4451
    0.3954   -0.3991    0.3932    0.1305         0   -0.6132    0.3697
    0.4708    0.3028    0.0500    0.2609   -0.7071   -0.0113   -0.3405
    0.3162    0.5016    0.1207   -0.7128    0.0000    0.0167    0.3543
    0.4708    0.3028    0.0500    0.2609    0.7071   -0.0113   -0.3405

>> rank(a)

ans =

     7

>> rank(s)

ans =

     7

>> % truncate the s matrix by keeping only first four singular values
>> news = [s(1:4,:);zeros([5,7])];
>> % i.e., the "new" s 

>> rank(news)

ans =

     4

>> repackage the a matrix
>> newu = [u(1:9,1:4)';zeros(5,9)]'
>> newv = [v(1:7,1:4)';zeros(3,7)]'
>> newa = newu*news*newv'

newa =

   -0.0018    0.5959   -0.0148    0.4522    0.6973    0.0102    0.6973
   -0.0723    0.4939    0.6254    0.0743    0.0121   -0.0133    0.0121
    0.0002   -0.0067    0.0052   -0.0013    0.3569    0.7036    0.3569
    0.1968    0.0512    0.0064    0.2179    0.0532   -0.0540    0.0532
   -0.0723    0.4939    0.6254    0.0743    0.0121   -0.0133    0.0121
    0.6315   -0.0598    0.0288    0.5291   -0.0008    0.0002   -0.0008
    0.0002   -0.0067    0.0052   -0.0013    0.3569    0.7036    0.3569
    0.2151    0.2482    0.4347    0.2262   -0.0359    0.0394   -0.0359
    0.6315   -0.0598    0.0288    0.5291   -0.0008    0.0002   -0.0008

>> % the above extra care is not really necessary
>> % we could have blindly multipled with the old u and v
>> % since the rank of news has been reduced, thus
>> newa = u*news*v'

newa =

   -0.0018    0.5959   -0.0148    0.4522    0.6973    0.0102    0.6973
   -0.0723    0.4939    0.6254    0.0743    0.0121   -0.0133    0.0121
    0.0002   -0.0067    0.0052   -0.0013    0.3569    0.7036    0.3569
    0.1968    0.0512    0.0064    0.2179    0.0532   -0.0540    0.0532
   -0.0723    0.4939    0.6254    0.0743    0.0121   -0.0133    0.0121
    0.6315   -0.0598    0.0288    0.5291   -0.0008    0.0002   -0.0008
    0.0002   -0.0067    0.0052   -0.0013    0.3569    0.7036    0.3569
    0.2151    0.2482    0.4347    0.2262   -0.0359    0.0394   -0.0359
    0.6315   -0.0598    0.0288    0.5291   -0.0008    0.0002   -0.0008

>> % notice that newa has negative entries and is pretty dense
>> % notice that it has new positive entries in the first column
>> % 0.1968 and 0.2151, which didn't exist before
>> % lets checkup if the norm differences match up
>> diffnorm = norm(a-newa,'fro')

diffnorm =

    0.9271

>> checkval = sqrt(s(5,5)*s(5,5)+s(6,6)*s(6,6)+s(7,7)*s(7,7))

checkval =

    0.9271

>> % AND SO ON, ENJOY!
>> % Try running some sample queries
>> % Do not forget to project the queries into the new space
>> % before comparison
