]> gitweb.michael.orlitzky.com - octave.git/blobdiff - rank_k_approximation.m
Add the spectral_radius() function.
[octave.git] / rank_k_approximation.m
index ff42f589c7a33e0eb09f1422ea43f10317b7d46a..444325902e646e3c37a5ad6d7931745cc7d94e64 100644 (file)
@@ -14,6 +14,20 @@ function Ak = rank_k_approximation(A,k)
   %
   %   - ``Ak`` -- The rank-k approximation to ``A``.
   %
-  [U, S, V] = svds(A, k);
-  Ak = U*S*V';
+  [m,n] = size(A);
+
+  if (k >= min(m,n))
+    % We're keeping greater than or equal to the maximum number of
+    % singular values that can exist.
+    Ak = A;
+    return;
+  end
+
+  [U, S, V, flag] = svds(A, k);
+
+  if (flag == 0)
+    Ak = U*S*V';
+  else
+    error('Could not compute the rank-k approximation.');
+  end
 end