F. Privé F. Privé - 3 years ago 137
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

s <- svd(M)

is a 4x4 matrix and
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 Source

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)


# [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))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download