F. Privé - 1 month ago 7

R Question

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`

`s$v`

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))
```

Source (Stackoverflow)

Comments