首页 > 解决方案 > 大代数R中的大矩阵运算

问题描述

我想将两个大矩阵相乘并将按列函数应用于生成的矩阵。鉴于我没有足够的内存来分配这些对象,我尝试了bigalgebra允许计算一些矩阵运算的包,但不是这里需要的。

我的脚本如下所示:

library(bigalgebra)

X <- as.big.matrix( matrix(rnorm(265225*360), nrow = 265225, ncol = 360) )
Y <- as.big.matrix( matrix(rnorm(360*10000),  nrow = 360,    ncol = 265225) )
Z <- X %*% Y # Z is a big.matrix object

apply(Z, 2, function(x) sqrt(sum(x^2)))
# > Error: in as.vector(data) : no method for coercing this s4 to class to a vector

apply(as.matrix(Z), 2, function(x) sqrt(sum(x^2)))
# > Error: cannot allocate vector of size 19.5 Gb

我该如何处理这个对象?而且,有没有更好/更快的方法来达到相同的结果?

标签: r

解决方案


推荐阅读