After performing SVD on a matrix, I would like to create a function (I'm not good with functions in R yet) that creates a reduced matrix per my specified n-value.
For instance, here is the R code for what I have now.
scores = c(3,1,1,-1,3,1)
Mat = matrix(scores, nrow=2)
svd = svd(Mat)
Now, to go from the matrix factorization to the original matrix, via brute force and ignorance, the following works.
score1 = svd$u[,1] %*% t(svd$v[,1]) * svd$d[1]
score2 = svd$u[,2] %*% t(svd$v[,2]) * svd$d[2]
z = score1 + score2
z
[,1] [,2] [,3]
[1,] 3 1 3
[2,] 1 -1 1
Going forward, I want to be able to do this on a large matrix and would like to be able to specify the number of factors. So, instead of summing scores 1:n, I want a function to do that for me.