I want to generate a matrix of 30000 x 30000 in r, multiplying a vector of 30000 elements by its transpose and then obtain SVD of that matrix, but the program tells me that r can not locate a vector of size 900000000. Help me, what can I do?
y <- read.csv("C:\\Users\\jmarescr\\Desktop\\BigLetra50.csv",header=TRUE)
x <- matrix(y[1:30000,1],30000,1)
tx <- as.matrix(t(x))
mat <- x %*% tx
Error: can not allocate vector of length 900000000
s <- svd(mat)
Error in svd (x): object 'mat' not found
Part of the beauty of the SVD is that you don't need to take the crossproduct of x
to get the crossproduct's SVD.
You can, instead, get the SVD of x%*%t(x)
(aka tcrossprod(x)
) directly from the elements of x
's SVD. Specifically (and up to the sign of U's columns) SVD(x %*% t(x)) = UD^2 t(U), where U and D are taken from the SVD of x
. (For a reference, see here .)
To see it in action, try out a smaller example:
set.seed(1)
x <- matrix(rnorm(15), ncol=5)
svd(x)$d
# [1] 3.046842 1.837539 1.411585
sqrt(svd(tcrossprod(x))$d)
# [1] 3.046842 1.837539 1.411585
svd(x)$u
# [,1] [,2] [,3]
# [1,] -0.3424029 0.7635281 0.5475264
# [2,] -0.8746155 -0.4719093 0.1111273
# [3,] 0.3432316 -0.4408248 0.8293766
svd(tcrossprod(x))$u
# [,1] [,2] [,3]
# [1,] -0.3424029 0.7635281 0.5475264
# [2,] -0.8746155 -0.4719093 0.1111273
# [3,] 0.3432316 -0.4408248 0.8293766
svd(tcrossprod(x))$v
# [,1] [,2] [,3]
# [1,] -0.3424029 0.7635281 0.5475264
# [2,] -0.8746155 -0.4719093 0.1111273
# [3,] 0.3432316 -0.4408248 0.8293766
Another way to see this:
sss <- svd(x)
with(sss, u %*% diag(d)^2 %*% t(u))
# [,1] [,2] [,3]
# [1,] 3.654154 1.684675 -1.322649
# [2,] 1.684675 7.877802 -1.900721
# [3,] -1.322649 -1.900721 3.120415
tcrossprod(x)
# [,1] [,2] [,3]
# [1,] 3.654154 1.684675 -1.322649
# [2,] 1.684675 7.877802 -1.900721
# [3,] -1.322649 -1.900721 3.120415
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.