# Matrix dimensions of the svd() output in R

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) head(recompose_M) head(M)  • @GeoMatt22 OP has wrongly reported the R's output. dim(U) # 3x100 is wrong, it is actually 100x3, as it should be. Oct 14, 2016 at 18:32 • (I edited to fix it.) Oct 14, 2016 at 18:36 ## 1 Answer 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.

• @GeoMatt22 you do not use R, so Matlab or python? Oct 14, 2016 at 17:39
• @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? Oct 14, 2016 at 17:45
• @GeoMatt22 please check this question I asked: R is automatically fixing the matrix multiplicatio for you... stackoverflow.com/questions/39025900/… Oct 14, 2016 at 17:48
• 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.) Oct 14, 2016 at 17:50
• @GeoMatt22 you are right, R will fix on vector but not matrix. Oct 14, 2016 at 18:01