%
% - ``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