]> gitweb.michael.orlitzky.com - octave.git/blobdiff - rank_k_approximation.m
Add rank_k_approximation() and its tests.
[octave.git] / rank_k_approximation.m
diff --git a/rank_k_approximation.m b/rank_k_approximation.m
new file mode 100644 (file)
index 0000000..ff42f58
--- /dev/null
@@ -0,0 +1,19 @@
+function Ak = rank_k_approximation(A,k)
+  %
+  % Compute the rank-k approximation to A from the ``k`` largest
+  % singular values of A.
+  %
+  % INPUT:
+  %
+  %   - ``A`` -- The matrix to approximate.
+  %
+  %   - ``k`` -- The rank of the resulting matrix; i.e. the number
+  %              of (large) singular values to keep.
+  %
+  % OUTPUT:
+  %
+  %   - ``Ak`` -- The rank-k approximation to ``A``.
+  %
+  [U, S, V] = svds(A, k);
+  Ak = U*S*V';
+end