F. Privé F. Privé - 1 month ago 7
R Question

Singular value decomposition in R

Following the example of wikipedia's page on SVD, I created the following matrix in R:

M <- matrix(0, 4, 5)
M[1, 1] <- 1
M[4, 2] <- 2
M[2, 3] <- 3
M[1, 5] <- 2


Computed the SVD from package
base
:

s <- svd(M)


Yet,
s$u
is a 4x4 matrix and
s$v
is a 5x4 matrix, whereas V should be a 5x5 matrix, as in Wikipedia's page (and other pages on the subject).

So, I'm a bit confused..

Answer

By default, R does not compute all the singular vectors. (Read the doc)

If you want to compute all of them, you can use svd's arguments nu and nv.

E.g., in your case:

s = svd(M, nv = 5)

Check:

dim(s$v)
# [1] 5 5

s$u %*% cbind(diag(s$d), rep(0,4)) %*% t(s$v)
# You get M.

More generally, you can get all the singular vectors this way:

s = svd(M, nu = nrow(M), nv = ncol(M))
Comments