1

Input is supposed to be a mxn matrix and a natural number k so that 0

I started with this code

function AK = svdapprox(A,k)
tol=0.001                                   
r=rank(A,tol)
if k>r           % return error message if k exceeds rank of matrix          
        fprintf('Value of k must not exceed that of r' ); 
end
[U, S, V]=svd(A)             %calculate the svd of a

To proceeed from here, I was thinking since number of singular values in s=rank A, i could limit the diagnoal of s to k elements and then make it return usv, but i couldn't find a way to make this work. As you can see I don't have much experience with matlab and I'm not sure if this is the best way to go about this. Any help would be appreciated.

J.Dawg
  • 81
  • Details and examples of low rank approximations are in https://math.stackexchange.com/questions/2138119/interpretation-of-svd-for-text-mining-topic-analysis/2275599#2275599, https://math.stackexchange.com/questions/2205651/low-rank-approximation-with-svd-on-a-kernel-matrix/2207592#2207592 – dantopa May 17 '17 at 17:03

1 Answers1

1

You want the first $k$ columns of $U$, the upper $k \times k$ part of $S$, and the first $k$ rows of $v$.

For this you want

 u = U(1:k, :)
 s = S(1:k, 1:k)
 v = V(:, 1:k)
Nitin
  • 2,948
  • 1
    IIRC Matlab gives V with the right singular vectors as columns, so you want those columns of V followed by a transpose or those rows of V'. – Ian Oct 28 '16 at 16:57