繁体   English   中英

R构造稀疏矩阵

[英]R constructing sparse Matrix

我正在阅读R中Matrix包的说明但是我无法理解函数中的p参数:

sparseMatrix(i = ep, j = ep, p, x, dims, dimnames,
         symmetric = FALSE, index1 = TRUE,
         giveCsparse = TRUE, check = TRUE)

根据http://stat.ethz.ch/R-manual/R-devel/library/Matrix/html/sparseMatrix.html

电话号码:
指针的数字(整数值)向量,每列(或行)一个,到列(或行)中元素的初始(从零开始)索引。 必须缺少i,j或p中的一个。

我认为p是针对行或列索引的压缩表示,因为在ij具有多个元素以具有相同的值来表示单个行/列是浪费的。 但是当我尝试提供的示例时,我仍然无法弄清楚p是如何控制x哪个元素进入哪个行/列的

dn <- list(LETTERS[1:3], letters[1:5])
## pointer vectors can be used, and the (i,x) slots are sorted if necessary:
m <- sparseMatrix(i = c(3,1, 3:2, 2:1), p= c(0:2, 4,4,6), x = 1:6, dimnames = dn)

只需稍微阅读一下?SparseMatrix来学习如何解释p (特别要注意关于p的“扩展形式”的一点。)

如果'i'或'j'缺失,则'p'必须是非递减整数向量,其第一个元素为零。 它提供行或列索引的压缩或“指针”表示,以缺少者为准。 'p','rep(seq_along(dp),dp)'的扩展形式,其中'dp < - diff(p)'用作(基于1的)行或列索引。

这是一个小功能,可以帮助您了解这在实践中意味着什么:

pex <- function(p) {
    dp <- diff(p)
    rep(seq_along(dp), dp)
}

## Play around with the function to discover the indices encoded by p.
pex(p = c(0,1,2,3))
# [1] 1 2 3

pex(p = c(0,0,1,2,3))
# [1] 2 3 4

pex(p = c(10,11,12,13))
# [1] 1 2 3

pex(p = c(0,0,2,5))
# [1] 2 2 3 3 3

pex(p = c(0,1,3,3,3,3,8))
# [1] 1 2 2 6 6 6 6 6

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM