具有对角线对 1 的矩阵

发布于 2025-01-02 00:36:15 字数 806 浏览 4 评论 0原文

假设我有以下矩阵 mat,它是一个二进制指示矩阵:

mat<-matrix(c(1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1), byrow=T ,nrow=3)

> mat
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1    1    0    0    0    0
[2,]    0    0    1    1    0    0
[3,]    0    0    0    0    1    1

这个矩阵只有3行。我需要创建一个包含 10000 行的行,对角线上具有相同的 1 对模式。 例如,对于 5 行,我期望一个 5 x 10 矩阵:

     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    1    1    0    0    0    0    0    0    0     0
[2,]    0    0    1    1    0    0    0    0    0     0
[3,]    0    0    0    0    1    1    0    0    0     0
[4,]    0    0    0    0    0    0    1    1    0     0
[5,]    0    0    0    0    0    0    0    0    1     1

有谁知道一个简单的方法来做到这一点? 多谢

Say I have the following matrix mat, which is a binary indicator matrix:

mat<-matrix(c(1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1), byrow=T, nrow=3)

> mat
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1    1    0    0    0    0
[2,]    0    0    1    1    0    0
[3,]    0    0    0    0    1    1

This matrix has only 3 rows. I need to create one with 10000 rows, with the same pattern of pairs of 1s on the diagonals.
E.g. for 5 rows, I expect a 5 x 10 matrix:

     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    1    1    0    0    0    0    0    0    0     0
[2,]    0    0    1    1    0    0    0    0    0     0
[3,]    0    0    0    0    1    1    0    0    0     0
[4,]    0    0    0    0    0    0    1    1    0     0
[5,]    0    0    0    0    0    0    0    0    1     1

Does anyone know a simple way to do that?
Thanks a lot

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

注定孤独终老 2025-01-09 00:36:15

这是一个稀疏矩阵,因此,引用非零条目会更好:这将节省 RAM 并使自动生成矩阵变得更容易。

每个条目的索引为 (i,j,x),分别指行、列和值。假设您想要填充 N(假设 N = 10)行,那么每行生成 2 个条目(在下面的代码中由 i 索引);每列仅使用一次,因此有 2*N 个唯一的列值。每个非零条目都是 1。

产生这个的代码是:

N = 10
i = rep(1:N, each = 2)
j = 1:(2*N)
v = 1

library(Matrix)
mat = sparseMatrix(i = i, j = j, x = v)

结果矩阵是:

> mat
10 x 20 sparse Matrix of class "dgCMatrix"

 [1,] 1 1 . . . . . . . . . . . . . . . . . .
 [2,] . . 1 1 . . . . . . . . . . . . . . . .
 [3,] . . . . 1 1 . . . . . . . . . . . . . .
 [4,] . . . . . . 1 1 . . . . . . . . . . . .
 [5,] . . . . . . . . 1 1 . . . . . . . . . .
 [6,] . . . . . . . . . . 1 1 . . . . . . . .
 [7,] . . . . . . . . . . . . 1 1 . . . . . .
 [8,] . . . . . . . . . . . . . . 1 1 . . . .
 [9,] . . . . . . . . . . . . . . . . 1 1 . .
[10,] . . . . . . . . . . . . . . . . . . 1 1

只需使用上面的代码并设置 N = 10000,你就会得到你的矩阵。

作为额外的好处:您想要的矩阵 (N = 1E5) 仅消耗 321424 字节。相比之下,使用数字(即 8 字节)条目的大小为 10K x 20K 的标准密集矩阵将占用 1.6GB。正如他们在“接触”中所说:这似乎是对空间的严重浪费,对吧?

This is a sparse matrix, and, as such, you'll do much better referencing the non-zero entries: this will save you RAM and make it easier to automatically generate the matrix.

Each entry is indexed as (i,j,x), referring to the row, column, and value. Suppose that you have N (say N = 10) rows that you'd like to fill, then you are producing 2 entries per row (indexed by i, in the code below); each column is used only once, so there are 2*N unique column values. Each non-zero entry is 1.

The code for producing this is:

N = 10
i = rep(1:N, each = 2)
j = 1:(2*N)
v = 1

library(Matrix)
mat = sparseMatrix(i = i, j = j, x = v)

The resulting matrix is:

> mat
10 x 20 sparse Matrix of class "dgCMatrix"

 [1,] 1 1 . . . . . . . . . . . . . . . . . .
 [2,] . . 1 1 . . . . . . . . . . . . . . . .
 [3,] . . . . 1 1 . . . . . . . . . . . . . .
 [4,] . . . . . . 1 1 . . . . . . . . . . . .
 [5,] . . . . . . . . 1 1 . . . . . . . . . .
 [6,] . . . . . . . . . . 1 1 . . . . . . . .
 [7,] . . . . . . . . . . . . 1 1 . . . . . .
 [8,] . . . . . . . . . . . . . . 1 1 . . . .
 [9,] . . . . . . . . . . . . . . . . 1 1 . .
[10,] . . . . . . . . . . . . . . . . . . 1 1

Just use the code above and set N = 10000, and you'll have your matrix.

As an added bonus: your desired matrix (N = 1E5) consumes only 321424 bytes. In contrast, a standard dense matrix of size 10K x 20K will take 1.6GB, using numeric (i.e. 8 byte) entries. As they said in "Contact": that seems like an awful waste of space, right?

情绪 2025-01-09 00:36:15

当您没有提供足够的元素来填充矩阵时,它们将被回收:如果您提供两个 1 和 n 个零(第一行和第二行的前两个元素),您将获得所需的矩阵。

n <- 5
matrix( 
  c(1,1,rep(0,2*n)), 
  byrow=TRUE, nr=n, nc=2*n 
)

When you do not provide enough elements to fill the matrix, they are recycled: if you provide two ones and n zeroes (the first row and the first two elements of the second row), you will get the desired matrix.

n <- 5
matrix( 
  c(1,1,rep(0,2*n)), 
  byrow=TRUE, nr=n, nc=2*n 
)
触ぅ动初心 2025-01-09 00:36:15

除非您打算在矩阵中填充许多其他值,否则您可能需要迭代器的稀疏矩阵解决方案。也就是说,这是生成矩阵的非稀疏版本的一种可爱方法:

double_diag <- function(n)
{
  matrix(rep(diag(n), each = 2), byrow = TRUE, nrow = n)
}
double_diag(5)

Unless you intend on filling many other values in the matrix, you probably want Iterator's sparse matrix solution. That said, here's a cute way of generating a non-sparse version of the matrix:

double_diag <- function(n)
{
  matrix(rep(diag(n), each = 2), byrow = TRUE, nrow = n)
}
double_diag(5)
定格我的天空 2025-01-09 00:36:15

@VincentZooneKynd 有一个很好的解决方案,但它给出了一个警告。这是避免警告的变体:

n <- 5
matrix(rep(c(1,1,rep(0,2*n)), len=2*n*n), n, byrow=TRUE)

@VincentZooneKynd has a nice solution, but it gives a warning. Here's a variant that avoids the warning:

n <- 5
matrix(rep(c(1,1,rep(0,2*n)), len=2*n*n), n, byrow=TRUE)
子栖 2025-01-09 00:36:15

狡猾:

> n <- 5
> t(model.matrix(~0+gl(n,2)))[,]
          1 2 3 4 5 6 7 8 9 10
gl(n, 2)1 1 1 0 0 0 0 0 0 0  0
gl(n, 2)2 0 0 1 1 0 0 0 0 0  0
gl(n, 2)3 0 0 0 0 1 1 0 0 0  0
gl(n, 2)4 0 0 0 0 0 0 1 1 0  0
gl(n, 2)5 0 0 0 0 0 0 0 0 1  1

Trickly:

> n <- 5
> t(model.matrix(~0+gl(n,2)))[,]
          1 2 3 4 5 6 7 8 9 10
gl(n, 2)1 1 1 0 0 0 0 0 0 0  0
gl(n, 2)2 0 0 1 1 0 0 0 0 0  0
gl(n, 2)3 0 0 0 0 1 1 0 0 0  0
gl(n, 2)4 0 0 0 0 0 0 1 1 0  0
gl(n, 2)5 0 0 0 0 0 0 0 0 1  1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文