使用R中的稀疏矩阵从向量中提取元素,而不转换为稠密矩阵
我想从向量使用R中的稀疏矩阵从向量中提取元素,而不转换为稠密矩阵,r,matrix,sparse-matrix,R,Matrix,Sparse Matrix,我想从向量x1中提取所有元素,其中第I列存在于稀疏矩阵中。我需要删除所有稀疏元素,但结果应该在它们自己的对象/列表/矩阵中逐行显示 给定: > x1 [1] 1 2 3 4 5 6 7 8 9 10 > sparse_mat 8 x 10 sparse Matrix of class "ngCMatrix" [1,] | | | . . . . . . . [2,] . | | | . . . . . . [3,] . . | | | . . . . . [4,
x1
中提取所有元素,其中第I列存在于稀疏矩阵中。我需要删除所有稀疏元素,但结果应该在它们自己的对象/列表/矩阵中逐行显示
给定:
> x1
[1] 1 2 3 4 5 6 7 8 9 10
> sparse_mat
8 x 10 sparse Matrix of class "ngCMatrix"
[1,] | | | . . . . . . .
[2,] . | | | . . . . . .
[3,] . . | | | . . . . .
[4,] . . . | | | . . . .
[5,] . . . . | | | . . .
[6,] . . . . . | | | . .
[7,] . . . . . . | | | .
[8,] . . . . . . . | | |
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 2 3 4
[3,] 3 4 5
[4,] 4 5 6
[5,] 5 6 7
[6,] 6 7 8
[7,] 7 8 9
[8,] 8 9 10
期望的结果:
> x1
[1] 1 2 3 4 5 6 7 8 9 10
> sparse_mat
8 x 10 sparse Matrix of class "ngCMatrix"
[1,] | | | . . . . . . .
[2,] . | | | . . . . . .
[3,] . . | | | . . . . .
[4,] . . . | | | . . . .
[5,] . . . . | | | . . .
[6,] . . . . . | | | . .
[7,] . . . . . . | | | .
[8,] . . . . . . . | | |
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 2 3 4
[3,] 3 4 5
[4,] 4 5 6
[5,] 5 6 7
[6,] 6 7 8
[7,] 7 8 9
[8,] 8 9 10
带有注释的更完整示例
库(矩阵)
图书馆(purrr)
x1[1,]| | |。
#> [2,] . | | | . . . . . .
#> [3,] . . | | | . . . . .
#> [4,] . . . | | | . . . .
#> [5,] . . . . | | | . . .
#> [6,] . . . . . | | | . .
#> [7,] . . . . . . | | | .
#> [8,] . . . . . . . | | |
#如果有更好的方法,请告知?
“dgCMatrix”类的mat_x1_mult_稀疏8 x 10稀疏矩阵
#>
#> [1,] 1 2 3 . . . . . . .
#> [2,] . 2 3 4 . . . . . .
#> [3,] . . 3 4 5 . . . . .
#> [4,] . . . 4 5 6 . . . .
#> [5,] . . . . 5 6 7 . . .
#> [6,] . . . . . 6 7 8 . .
#> [7,] . . . . . . 7 8 9 .
#> [8,] . . . . . . . 8 9 10
#这很好,但不能与keep一起使用?
#mat_x1_mult_sparse[1,drop=FALSE]
#理想的结果,但这种方法我认为我失去了稀疏矩阵的优势?
mat_x1_mult_sparse[1,]%>%keep(~.x!=0)
#> [1] 1 2 3
mat_x1_mult_sparse[2,]%>%keep(~.x!=0)
#> [1] 2 3 4
#等等。。。
mat_x1_mult_sparse[8,]%>%keep(~.x!=0)
#> [1] 8 9 10
一个选项是使用摘要
方法获取非稀疏元素的索引
library(Matrix)
i1 <- summary(sparse_mat)
i2 <- as.matrix(i1[order(i1[,1]),]) # order by the row index
# multiply the sparse matrix by the replicated 'x1', extract elements
# with i2 index and convert it to n column matrix
matrix((sparse_mat * x1[col(sparse_mat)])[i2], ncol = 3, byrow = TRUE)
#. [,1] [,2] [,3]
#[1,] 1 2 3
#[2,] 2 3 4
#[3,] 3 4 5
#[4,] 4 5 6
#[5,] 5 6 7
#[6,] 6 7 8
#[7,] 7 8 9
#[8,] 8 9 10
库(矩阵)
i1当我注意到你们不想让你们的矩阵变得稀疏时,我删除了前面的答案;不过,我们的想法是利用矩阵的i
槽:
# convert to dgCMatrix since ngCMatrix can only be on/off
out = as(sparse_mat, 'dgCMatrix')
# subset to the "on" elements of sparse_mat,
# and replace with the column number. The column number is
# not stored directly so we have to make it ourselves, basically
# by looking for when the value in @i stays the same or goes down
out[sparse_mat] = c(1L, cumsum(diff(sparse_mat@i) <= 0) + 1L)
out
# 8 x 10 sparse Matrix of class "dgCMatrix"
#
# [1,] 1 2 3 . . . . . . .
# [2,] . 2 3 4 . . . . . .
# [3,] . . 3 4 5 . . . . .
# [4,] . . . 4 5 6 . . . .
# [5,] . . . . 5 6 7 . . .
# [6,] . . . . . 6 7 8 . .
# [7,] . . . . . . 7 8 9 .
# [8,] . . . . . . . 8 9 10
#转换为dgCMatrix,因为ngCMatrix只能打开/关闭
out=as(稀疏矩阵'dgCMatrix')
#稀疏矩阵“on”元素的子集,
#并替换为列号。列号为
#不直接储存,所以我们必须自己制作,基本上
#通过查找@i中的值何时保持不变或下降
out[sparse_mat]=c(1L,求和)diff(sparse_mat@i)将矩阵(na.省略(c(t)(替换(col(sparse_-mat),其中(!sparse_-mat),na))),ncol=3,byrow=TRUE)
this help?@akrun,很有趣,可能是这样,但我应该更清楚或者提供一个更好的例子。如果x1抱歉,我以为你在使用列索引,那么这没有帮助。在这种情况下,使用summary
获取索引,即i1@akrun,我相信这会奏效。请随意发布为答案。有没有比使用mat\u x1\u mult\u sparse更好的方法来创建mat\u x1\u mult\u sparse而不是进行双转置,可以复制“x1”并进行乘法,即sparse\u mat*x1[col(sparse\u mat)]