]> gitweb.michael.orlitzky.com - octave.git/blob - rank_k_approximation.m
Add classical_iteration() sans comments, refactor jacobi() to use it.
[octave.git] / rank_k_approximation.m
1 function Ak = rank_k_approximation(A,k)
2 %
3 % Compute the rank-k approximation to A from the ``k`` largest
4 % singular values of A.
5 %
6 % INPUT:
7 %
8 % - ``A`` -- The matrix to approximate.
9 %
10 % - ``k`` -- The rank of the resulting matrix; i.e. the number
11 % of (large) singular values to keep.
12 %
13 % OUTPUT:
14 %
15 % - ``Ak`` -- The rank-k approximation to ``A``.
16 %
17 [m,n] = size(A);
18
19 if (k >= min(m,n))
20 % We're keeping greater than or equal to the maximum number of
21 % singular values that can exist.
22 Ak = A;
23 return;
24 end
25
26 [U, S, V, flag] = svds(A, k);
27
28 if (flag == 0)
29 Ak = U*S*V';
30 else
31 error('Could not compute the rank-k approximation.');
32 end
33 end