SVDS function gives different result for MATLAB and python

Viewed 75

I am currently changing a MATLAB code to python because I need to work in a python environment. But the result I get for calculating the singular values and vectors is different in value and size for MATLAB and python. For example, for a 5 by 5 identity matrix C:

In MATLAB,

U, S, T = svds(C, 5)

gives matrices with shapes: U - (15000, 5), S - (3, 5), T - (5, 1) whereas

In python,

U, S, T = scipy.sparse.linalg.svds(C, k=4)

gives matrices with shapes: U - (5, 5), S - (4, ), T - (4, 5)

The value of k in python has to be 4 because k must be less than the minimum of the shape of C. Is there any way I can get the same results as in Matlab? Any help is appreciated. Thank you in advance!

1 Answers

The SVD isn't unique in its values and their order but its dimensions are unique and set well. Though the representation could be different (e.g. whether or not to give the vectors for zero singular values, output the singular values as a vector or diagonal matrix, etc...).

In most cases MATLAB's svd and scipy's svd will match.

When working on sparse data the output won't be, usually, the whole SVD. Then some inner choices of the algorithms might give us different results.

Since both MATLAB and scipy give the largest singular values if you'd ask for the same number of values you'll get the same result.

Your assertion:

The value of k in python has to be 4 because k must be less than the minimum of the shape of C.

Is wrong since you matrix is 5x5 so you may use k=5 just like in MATLAB.

Related