-U = [4/5, 3/5, 0 ; ...
- 0 , 4/5, 3/5; ...
- 3/5, 0 , 4/5 ];
+U = [ 0, 0, 1; ...
+ 0, 1, 0; ...
+ 1, 0, 0 ];
-S = [ 1, 0, 0;
- 0, 2, 0;
- 0, 0, 3 ];
+S = [ 3, 0, 0; ...
+ 0, 2, 0; ...
+ 0, 0, 1 ];
-V = [ 0, 1, 0;
- 1, 0, 0;
+V = [ 0, 1, 0; ...
+ 1, 0, 0; ...
0, 0, 1 ];
A = U*S*V';
expected = A;
+% This should be exact.
actual = rank_k_approximation(A, 3);
unit_test_equals("Full rank approximation of a matrix is itself", ...
- expected, ...
- actual);
+ actual, ...
+ expected);
+
+
+
+% This too.
+actual = rank_k_approximation(A, 13);
+
+unit_test_equals("Full rank approximation of a matrix is itself", ...
+ actual, ...
+ expected);
+
+
+S2 = S;
+S2(3,3) = 0;
+expected = U*S2*V';
+actual = rank_k_approximation(A, 2);
+unit_test_equals("Rank 2 approximation of a matrix is correct", ...
+ true, ...
+ (norm(actual - expected) <= 1e-10));
+
+
+S1 = S2;
+S1(2,2) = 0;
+expected = U*S1*V';
+actual = rank_k_approximation(A, 1);
+unit_test_equals("Rank 1 approximation of a matrix is correct", ...
+ true, ...
+ (norm(actual - expected) <= 1e-10));