R构造稀疏矩阵 [英] R constructing sparse Matrix

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

问题描述

我正在阅读R中的Matrix软件包的指令.但是我无法理解函数中的p参数:

I'm reading through instructions of Matrix package in R. But I couldn't understand the p argument in function:

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

p:
指针的数字(整数值)向量,每一列(或每一行)指向 列(或行)中元素的初始(从零开始)索引. i,j或p之一 必须丢失.

p:
numeric (integer valued) vector of pointers, one for each column (or row), to the initial (zero-based) index of elements in the column (or row). Exactly one of i, j or p must be missing.

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

I figured p is for compressed representation of either the row or column indices because it's wasteful to have multiple elements in either i or j to have the same value to represent a single row/column. But when I tried the example provided, I still couldn't figure out how p is controlling which element of x goes to which row/column

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的扩展形式".)

Just read a bit farther down in ?SparseMatrix to learn how p is interpreted. (In particular, note the bit about the "expanded form" of p.)

如果缺少"i"或"j",则"p"必须为不可减小的整数 第一个元素为零的向量.它提供了压缩的 或指针"表示行或列的索引, 无论哪个都不见. "p"的扩展形式, "rep(seq_along(dp),dp)",其中"dp<-diff(p)"用作 (从1开始)行或列索引.

If ‘i’ or ‘j’ is missing then ‘p’ must be a non-decreasing integer vector whose first element is zero. It provides the compressed, or "pointer" representation of the row or column indices, whichever is missing. The expanded form of ‘p’, ‘rep(seq_along(dp),dp)’ where ‘dp <- diff(p)’, is used as the (1-based) row or column indices.

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

Here is a little function that will help you see what that means in practice:

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

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

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