From the second paragraph here: https://en.wikipedia.org/wiki/Singular_value_decomposition

SVD of an m x n matrix M with m columns and n rows = UDV* where

U is m x m 
D is m x n 
V is n x n

using some dummy data and R

M =data.frame(x = seq(1,100,1), y = seq(101,200,1), z =seq(301,400,1))
dim(M) # here m=3 and n = 100
D = diag(svd(M)$d) #  singular vectors
percentVariance2 = svd(M)$d^2/sum(svd(M)$d^2)
U = svd(M)$u # right singular vectors --eigenvectors
V = svd(M)$v # left singular vectors

dim(U) # 100x3
dim(D) # 3x3
dim(V) # 3x3

So when I run SVD in R on M which is m=3 and n =100 I get

U is n x m 
D is m x m 
V is m x m

Why are the dimensions different from the reference?

Notice the original data can still be obtained using U,D and V

recompose_M = U %*% D %*% t(V)
  • $\begingroup$ @GeoMatt22 OP has wrongly reported the R's output. dim(U) # 3x100 is wrong, it is actually 100x3, as it should be. $\endgroup$ – amoeba Oct 14 '16 at 18:32
  • $\begingroup$ (I edited to fix it.) $\endgroup$ – amoeba Oct 14 '16 at 18:36

This is because it does not make sense to find more singular values than the amount of rows or columns. Because of this the default in R only computes min(n, p) singular vectors where n = nrow(x) and p = ncol(x).

If you really want to you can change this behavior by changing the parameters of svd:

M =data.frame(x = seq(1,100,1), y = seq(101,200,1), z =seq(301,400,1))
dim(M) # here m=3 and n = 100
uvd = svd(M, nu = 100, nv = 3) 
D = diag(uvd$d)
percentVariance2 = D^2/sum(D^2)
U = uvd$u # right singular vectors 
V = uvd$v # left singular vectors

But notice that only the first 3 components make sense.

  • $\begingroup$ @GeoMatt22 you do not use R, so Matlab or python? $\endgroup$ – hxd1011 Oct 14 '16 at 17:39
  • $\begingroup$ @hxd1011 mainly Matlab, but starting to learn Python. So the matrix dimensions in the OP make sense to you? i.e. multiply U*D = [3 x 100]*[3 x 3]? Matlab would certainly give an error. Is it to do with something like C vs. Fortran array storage/indexing order? $\endgroup$ – GeoMatt22 Oct 14 '16 at 17:45
  • $\begingroup$ @GeoMatt22 please check this question I asked: R is automatically fixing the matrix multiplicatio for you... stackoverflow.com/questions/39025900/… $\endgroup$ – hxd1011 Oct 14 '16 at 17:48
  • 1
    $\begingroup$ Note that "it does not make sense to find more singular values than the [matrix rank]" is literally true. But there are certainly cases where it makes sense to find more singular vectors, which form a basis for the matrix's null space. (For example, in linear-equality constrained optimization. In practice QR decomposition would typically be used rather than full-on SVD, of course.) $\endgroup$ – GeoMatt22 Oct 14 '16 at 17:50
  • $\begingroup$ @GeoMatt22 you are right, R will fix on vector but not matrix. $\endgroup$ – hxd1011 Oct 14 '16 at 18:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.