File Exchange

image thumbnail

Simple SVD

version (978 Bytes) by Paul Godfrey
SVD computation using QR decomposition


Updated 23 Oct 2006

No License

A very short and simple program that computes the singular value decomposition of a matrix using the QR decomposition.

Comments and Ratings (20)


If my code has data something like following,

-0.11 0.02 -0.36 -0.11 0.48 -0.2
-0.3 -0.37 -0.18 -0.09 -0.16 -0.16
0.5 0.18 0.41 NaN NaN 0.15
0 -0.11 NaN 0.19 0 -0.07
0.4 -0.16 0.31 -0.34 0.02 0.21
0.39 -0.18 -0.09 -0.04 -0.25 -0.69
0.5 -0.24 -0.16 NaN 0.04 NaN
-0.52 -0.48 -0.73 -0.42 -0.4 -0.01
-0.87 -0.17 0.1 -0.12 NaN 0.52

why am I getting missing values in E^16? It is such a small value :(
-0.11 0.02 -0.36 -0.11 0.48 -0.2
-0.3 -0.37 -0.18 -0.09 -0.16 -0.16
0.5 0.18 0.41 -9.20913E-15 1.8946E-14 0.15
2.81661E-16 -0.11 4.57794E-16 0.19 -9.6239E-15 -0.07
0.4 -0.16 0.31 -0.34 0.02 0.21
0.39 -0.18 -0.09 -0.04 -0.25 -0.69
0.5 -0.24 -0.16 -1.53663E-14 0.04 -4.09478E-14
-0.52 -0.48 -0.73 -0.42 -0.4 -0.01
-0.87 -0.17 0.1 -0.12 1.8092E-13 0.52


prince liu

It is a very nice SVD algorithm. Thank the author very much. However, I cannot find a paper that introduces this method. Would you please help me ?

prince liu


very nice and simple


Thank you for submission. It appears that svdsim() requires a significant improvement in speed.
Built-in svd() takes <30 seconds for 6000x3000 matrix.
svdsim() has been running for over 5 minutes with the same matrix (with default tol). I had to stop the process.

n gh


Very nice SVD-Algorithm. Thank you!
But where can I find literature about this Method?

hendra togi

i have a project (Robust DWT-SVD Domain Image Watermarking:
Embedding Data in All Frequencies).
i use DWT, decompose the cover image A into 4 subbands:
LL, HL, LH, and HH.
can you tell me how to apply svd to each subband image…
the program uses matlab. thx


Could anybody tell me how to evaluate the time required by the SVD. Please kindly note the svd.m called directly by the workspace is a built-in function, which is much faster than the svd.m available on the Matlab website. So which one is more suitable to evaluate the required time?

Actually, I am trying to compare a new algorithm with the SVD in computational cost or time. I suppose the built-in SVD function might be faster than the source-code SVD function. Could anybody help me? Thanks a lot for your kindly helps in advance! My email address is

Sondur Lakshmipathi

My sincere thanks to the author. Very useful method. I compared the result with matlab inbuilt function on SVD, both match well. I had done in my application QR decomposition, I thought of reusing it for SVD computation, this helped me a lot. Good work.

Paul Godfrey

A few comments by the author...

To Scott Miller: Matlab's own SVD should always be, for many reasons, the program of first choice most SVD needs.
The purpose of this program was to demonstrate a very simple way to compute the SVD that was probably not well known.
To John D'Errico: Your first set of comments appear to be based on the assumption that this code is intended to compute with Matlab's own SVD code for all user applications. It is not.
Upon typing 'Help svdsim; the used clearly sees "A simple program that demonstrates..."
As such, I did not, originally, bother with a robust exit scheme since the program intent was algorithmic in nature and not efficiency oriented. However, I have updated the program to add a more sophiticated exit mechanism.
For your second comment and for the comments of Carlos López: The program could certainly be used to refine an existing SVD solution.
The required code would be trivial to add.
Also, QR decomposition routines (MGS) are easy to write by almost any programmer. However, an SVD routine is a more daunting task and is usually sent to a canned library routine.
This program provides an easy to implement algorithm that can be used whenever a more sophisticated math library is not available.

Carlos López

I consider seriously your comments, John. Thus, I will pose a different but related question to the readers: Is there a better algorithm to improve an estimate of the SVD than the one provided by this submission?

John D'Errico

While future higher precision computations might allow refinement using the scheme in this code, if higher precision is available in the future, then the svd code itself would far more efficiently call a higher precision version of svd at relatively little cost in time. An expensive refinement scheme would seem to be of little value.

Carlos López

Aside for dealing with sparse matrix, there might be another application for this code: improving an already available approximation of the real solution. Apparently svd does not take advantage of any first guess.
I suggest something like
To test this, you can use something like
to have an initial value, and later invoke the svdsim routine. A comparison might be made with the standard svd.
I foresee an application for higher-than-double precision calculations within matlab. In such case, the output of the standard svd can be improved to the required precision with a routine similar to svdsim.

John D'Errico

This would appear to have NO advantages over svd. It will be immensely slower than svd. One exception - in theory, svdsim will work on sparse matrices. However, svdsim is so slow that even here you are far better off converting your matrix to a full one, then calling svd. For example:

>> X = sprand(50,50,.01);
>> tic,[u,s,v]= svdsim(X);toc
Elapsed time is 4.720246 seconds.
>> tic,[u,s,v]= svd(full(X));toc
Elapsed time is 0.002718 seconds.

Note, this gets far worse if X is less sparse.

>> X = sprand(50,50,.1);
>> tic,[u,s,v]= svdsim(X);toc
Elapsed time is 41.527362 seconds.
>> tic,[u,s,v]= svd(full(X));toc
Elapsed time is 0.012801 seconds.

Another problem with this code is the lack of a convergence tolerance. It merely loops an arbitrary number of times, then quits.

So don't use this code to compute the svd. Just use svd.

This leaves only one purpose - an expository one. It does that reasonably well, but even there the lack of any convergence criterion suggests a low rating. Good expository code might also suggest why there is no sort at the end on the singular values, and provide a reference for the student to read further on methods to compute the SVD.

I'll raise my rating with improvements in the exposition and code.

Scott Miller

What are the advantages and disadvantages of using this routine over the function svd included in MATLAB?



Added an improved stopping criteria.

MATLAB Release Compatibility
Created with R13
Compatible with any release
Platform Compatibility
Windows macOS Linux