从较小的矩阵创建一个较大的矩阵

发布于 2024-09-27 09:29:57 字数 761 浏览 4 评论 0原文

我有一个矩阵 A,它是:

A <- matrix(c(1:15), byrow=T, nrow=5)
A
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    7    8    9
[4,]   10   11   12
[5,]   13   14   15

现在我想创建一个矩阵 B,它的尺寸为 8x8(或 10x10,或 15x15 等),看起来像这样:

     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]    1    2    3    0    0    0    0    0
[2,]    4    5    6    0    0    0    0    0
[3,]    7    8    9    0    0    0    0    0
[4,]   10   11   12    0    0    0    0    0
[5,]   13   14   15    0    0    0    0    0
[6,]    0    0    0    0    0    0    0    0
[7,]    0    0    0    0    0    0    0    0
[8,]    0    0    0    0    0    0    0    0

所以从 A 开始,我想添加列和行,到 8x8 尺寸,全部替换为零值...... 有什么想法吗? 先感谢您!!

I have a matrix A which is:

A <- matrix(c(1:15), byrow=T, nrow=5)
A
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    7    8    9
[4,]   10   11   12
[5,]   13   14   15

Now I want to create a matrix B, which is 8x8 dimensions (or 10x10, or 15x15, etc), which would look like this:

     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]    1    2    3    0    0    0    0    0
[2,]    4    5    6    0    0    0    0    0
[3,]    7    8    9    0    0    0    0    0
[4,]   10   11   12    0    0    0    0    0
[5,]   13   14   15    0    0    0    0    0
[6,]    0    0    0    0    0    0    0    0
[7,]    0    0    0    0    0    0    0    0
[8,]    0    0    0    0    0    0    0    0

So starting from A, I want to add columns and rows, to an 8x8 dimensions, all replaced with zero values...
Any idea?
Thank you in advance!!

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

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

发布评论

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

评论(3

一张白纸 2024-10-04 09:29:57

另一种方法是首先创建较大的矩阵,然后插入较小的矩阵,如以下函数所示:

    expandR = function(m,nrows,ncols,with=0){
      p=matrix(with,nrows,ncols)
      p[1:nrow(m),1:ncol(m)]=m
      p
    }

不确定哪个更好(更快、更干净等)。如果您尝试扩展到小于原始大小,则会出现错误。

Another way is to first create the bigger matrix, and then slot the small one in, as in this function:

    expandR = function(m,nrows,ncols,with=0){
      p=matrix(with,nrows,ncols)
      p[1:nrow(m),1:ncol(m)]=m
      p
    }

Not sure which is better (faster, cleaner etc). Mine errors if you try and expand to a size that's smaller than the original.

栀梦 2024-10-04 09:29:57

尝试假设 A 至少有一行和一列:

B <- matrix(0, 8, 8)
B[1:nrow(A), 1:ncol(A)] <- A

或者作为单个语句:

B <- "[<-"(matrix(0, 8, 8), 1:nrow(A), 1:ncol(A), value = A)

如果 A 可以有零行或零列,则使用 seq_len(nrow (A))seq_len(ncol(A)) 代替 1:nrow(A)1:ncol(A)< /代码>。

或者,即使在 A 具有零行或零列的情况下,这也有效:

B <- matrix(0, 8, 8)
B[cbind(c(row(A)), c(col(A)))] <- A

B <- "[<-"(matrix(0, 8, 8), cbind(c(row(A)), c(col(A))), value = A)

B <- replace(matrix(0, 8, 8), cbind(c(row(A)), c(col(A))), A)

Try this assuming A has at least one row and one column:

B <- matrix(0, 8, 8)
B[1:nrow(A), 1:ncol(A)] <- A

or as a single statement:

B <- "[<-"(matrix(0, 8, 8), 1:nrow(A), 1:ncol(A), value = A)

If A can have zero rows or zero columns then use seq_len(nrow(A)) and seq_len(ncol(A)) in place of 1:nrow(A) and 1:ncol(A).

Alternately, this works even in the case that A has zero rows or columns:

B <- matrix(0, 8, 8)
B[cbind(c(row(A)), c(col(A)))] <- A

or

B <- "[<-"(matrix(0, 8, 8), cbind(c(row(A)), c(col(A))), value = A)

or

B <- replace(matrix(0, 8, 8), cbind(c(row(A)), c(col(A))), A)
网白 2024-10-04 09:29:57

怎么样:

A <- matrix(c(1:15), byrow=T, nrow=5)

expandMatrix <- function(X, nrow, ncol) {
    X <- cbind(X, matrix(0, nrow = nrow(X), ncol = ncol - ncol(X)))
    X <- rbind(X, matrix(0, nrow = nrow - nrow(X), ncol = ncol(X)))
    X
}

然后

> expandMatrix(A, 8, 8)
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]    1    2    3    0    0    0    0    0
[2,]    4    5    6    0    0    0    0    0
[3,]    7    8    9    0    0    0    0    0
[4,]   10   11   12    0    0    0    0    0
[5,]   13   14   15    0    0    0    0    0
[6,]    0    0    0    0    0    0    0    0
[7,]    0    0    0    0    0    0    0    0
[8,]    0    0    0    0    0    0    0    0

或者

> expandMatrix(A, 10, 10)
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    1    2    3    0    0    0    0    0    0     0
 [2,]    4    5    6    0    0    0    0    0    0     0
 [3,]    7    8    9    0    0    0    0    0    0     0
 [4,]   10   11   12    0    0    0    0    0    0     0
 [5,]   13   14   15    0    0    0    0    0    0     0
 [6,]    0    0    0    0    0    0    0    0    0     0
 [7,]    0    0    0    0    0    0    0    0    0     0
 [8,]    0    0    0    0    0    0    0    0    0     0
 [9,]    0    0    0    0    0    0    0    0    0     0
[10,]    0    0    0    0    0    0    0    0    0     0

如果您最想要方阵作为输出,也可以给它一个默认值:

expandMatrix <- function(X, nrow, ncol = nrow) {
    X <- cbind(X, matrix(0, nrow = nrow(X), ncol = ncol - ncol(X)))
    X <- rbind(X, matrix(0, nrow = nrow - nrow(X), ncol = ncol(X)))
    X
}

然后 expandMatrix(A, 8) 就足够了。

How about:

A <- matrix(c(1:15), byrow=T, nrow=5)

expandMatrix <- function(X, nrow, ncol) {
    X <- cbind(X, matrix(0, nrow = nrow(X), ncol = ncol - ncol(X)))
    X <- rbind(X, matrix(0, nrow = nrow - nrow(X), ncol = ncol(X)))
    X
}

Then

> expandMatrix(A, 8, 8)
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]    1    2    3    0    0    0    0    0
[2,]    4    5    6    0    0    0    0    0
[3,]    7    8    9    0    0    0    0    0
[4,]   10   11   12    0    0    0    0    0
[5,]   13   14   15    0    0    0    0    0
[6,]    0    0    0    0    0    0    0    0
[7,]    0    0    0    0    0    0    0    0
[8,]    0    0    0    0    0    0    0    0

or

> expandMatrix(A, 10, 10)
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    1    2    3    0    0    0    0    0    0     0
 [2,]    4    5    6    0    0    0    0    0    0     0
 [3,]    7    8    9    0    0    0    0    0    0     0
 [4,]   10   11   12    0    0    0    0    0    0     0
 [5,]   13   14   15    0    0    0    0    0    0     0
 [6,]    0    0    0    0    0    0    0    0    0     0
 [7,]    0    0    0    0    0    0    0    0    0     0
 [8,]    0    0    0    0    0    0    0    0    0     0
 [9,]    0    0    0    0    0    0    0    0    0     0
[10,]    0    0    0    0    0    0    0    0    0     0

Could also give it a default if you mostly want square matrix as outputs:

expandMatrix <- function(X, nrow, ncol = nrow) {
    X <- cbind(X, matrix(0, nrow = nrow(X), ncol = ncol - ncol(X)))
    X <- rbind(X, matrix(0, nrow = nrow - nrow(X), ncol = ncol(X)))
    X
}

Then expandMatrix(A, 8) would suffice.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文