Skip to content

Instantly share code, notes, and snippets.

@ayota
Created April 19, 2015 18:04
Show Gist options
  • Save ayota/05fe6e618c4221251c65 to your computer and use it in GitHub Desktop.
Save ayota/05fe6e618c4221251c65 to your computer and use it in GitHub Desktop.
hwk12 2.rmd
2.
```{r}
#make matrices
set.seed(693)
A <- matrix(ceiling(runif(4, min=100, max=999)), nrow=2, ncol=2)
B <- matrix(ceiling(runif(4, min=-9, max=9)), nrow=2, ncol=2)
C <- matrix(ceiling(-runif(4, min=100, max=999)), nrow=2, ncol=2)
Z <- matrix(0, nrow=2, ncol=2)
M <- rbind(cbind(Z,Z,A), cbind(Z,B,Z), cbind(C,Z,Z))
M
```
a. While U and V have the same dimensions of M, the nonzero values do not follow the same pattern as M and so it is hard to associate certain groups of movies with certain groups of people.
```{r}
M.svd <- svd(M)
M.svd$u
M.svd$v
M.svd$d
```
b. As shown below, the $M_k$ matrix begins to look like M at k=5. Before that, the middle set of values is missing.
```{r}
genMs <- function(M) {
M.svd <- svd(M)
M.j <- lapply(1:ncol(S), function(j) diag(M.svd$d)[j,j] * (M.svd$u[,j] %*% t(M.svd$v[,j])))
sums <- Reduce("+",M.j, accumulate=TRUE)
return(sums)
}
genMs(M=M)
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment