大稀疏矩阵到三角矩阵R [英] Large Sparse Matrix to Triangular Matrix R

查看:145
本文介绍了大稀疏矩阵到三角矩阵R的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在R中有一个非常大的(约9100万个非零条目)sparseMatrix(),看起来像:

I have a very large (about 91 million non-zero entries) sparseMatrix() in R that looks like:

> myMatrix 
  a b c  
a . 1 2
b 1 . .
c 2 . .

我想将其转换为三角形矩阵(上或下),但是当我尝试myMatrix = myMatrix * lower.tri(myMatrix)时,出现一个错误,即问题对于lower.tri(太大) ).想知道是否有人知道解决方案.谢谢你的帮助!

I would like to convert it to a triangular matrix (upper or lower), but when I try myMatrix = myMatrix * lower.tri(myMatrix) there is an error that the 'problem is too large' for lower.tri(). Wondering if anyone might know of a solution. Thanks for any help!

推荐答案

而不是处理矩阵本身,而是处理其summary:

Instead of working on the matrix itself, work on its summary:

library(Matrix)
myMatrix <- sparseMatrix(
    i = c(1,1,2,3),
    j = c(2,3,1,1),
    x = c(1,2,1,2))

myMatrix
# 3 x 3 sparse Matrix of class "dgCMatrix"
#           
# [1,] . 1 2
# [2,] 1 . .
# [3,] 2 . .

mat.summ   <- summary(myMatrix)
lower.summ <- subset(mat.summ, i >= j)

sparseMatrix(i = lower.summ$i,
             j = lower.summ$j,
             x = lower.summ$x,
             dims = dim(myMatrix))
# 3 x 3 sparse Matrix of class "dgCMatrix"
#           
# [1,] . . .
# [2,] 1 . .
# [3,] 2 . .

这篇关于大稀疏矩阵到三角矩阵R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆