如何在R中的seq中插入零

时间:2016-03-17 20:00:48

标签: r seq

我需要得到这个:

      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,]    1    0    2    0    3    0    4    0    5
[2,]    0    0    0    0    0    0    0    0    0
[3,]    6    0    7    0    8    0    9    0   10
[4,]    0    0    0    0    0    0    0    0    0
[5,]   11    0   12    0   13    0   14    0   15
[6,]    0    0    0    0    0    0    0    0    0
[7,]   16    0   17    0   18    0   19    0   20
[8,]    0    0    0    0    0    0    0    0    0
[9,]   21    0   22    0   23    0   24    0   25

我写道:

  n<-5
  x <- c(1,0,2,0,3,0,4,0,5,0,0,0,0,0,0,0,0,0,6,0,7,0,8,0,9,0,10,0,0,0,0,0,0,0,0,0,11,0,12,0,13,0,14,0,15,0,0,0,0,0,0,0,0,0,16,0,17,0,18,0,19,0,20,0,0,0,0,0,0,0,0,0,21,0,22,0,23,0,24,0,25)
  x
  M<-matrix(x,ncol=n+(n-1),byrow=TRUE)
  length(x)
  M

那么,我可以使用n来获取此信息吗?例如:x <- 1:n^2 ??也就是说,我希望在矩阵中有seq(n^2),但是我需要带零的行和带零的行?

我希望我的问题是可以理解的,非常感谢你:)。

4 个答案:

答案 0 :(得分:16)

n ...

开始
tm = matrix(0L, 2*n-1, 2*n-1)
tm[(col(tm) %% 2) & (row(tm) %% 2)] <- seq(n^2)
m = t(tm)

这不是最快捷的方式,我确定,但确实有效:

      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
 [1,]    1    0    2    0    3    0    4    0    5
 [2,]    0    0    0    0    0    0    0    0    0
 [3,]    6    0    7    0    8    0    9    0   10
 [4,]    0    0    0    0    0    0    0    0    0
 [5,]   11    0   12    0   13    0   14    0   15
 [6,]    0    0    0    0    0    0    0    0    0
 [7,]   16    0   17    0   18    0   19    0   20
 [8,]    0    0    0    0    0    0    0    0    0
 [9,]   21    0   22    0   23    0   24    0   25

或者,为了避免转置,请使用which

m = matrix(0L, 2*n-1, 2*n-1)
w = which((col(m) %% 2) & (row(m) %% 2), arr.ind=TRUE)
m[w[,2:1]] <- seq(n^2)

答案 1 :(得分:10)

您可以使用Kronecker product

n <- 5
m1 <- matrix(seq(n^2), ncol = n, byrow = TRUE)
m2 <- matrix(c(1,0,0,0), ncol = 2)
m3 <- kronecker(m1, m2) # this can also be written as m3 <- m1 %x% m2
m3 <- m3[-nrow(m3),-ncol(m3)]
#> m3
#      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
# [1,]    1    0    2    0    3    0    4    0    5
# [2,]    0    0    0    0    0    0    0    0    0
# [3,]    6    0    7    0    8    0    9    0   10
# [4,]    0    0    0    0    0    0    0    0    0
# [5,]   11    0   12    0   13    0   14    0   15
# [6,]    0    0    0    0    0    0    0    0    0
# [7,]   16    0   17    0   18    0   19    0   20
# [8,]    0    0    0    0    0    0    0    0    0
# [9,]   21    0   22    0   23    0   24    0   25

答案 2 :(得分:6)

此解决方案首先在OP的代码中创建向量x,然后转换为矩阵:

n <- 5
m <- 2*n - 1
x <- rep_len(c(rep_len(c(1,0), m), rep(0, m)), m^2)
x[x==1] <- 1:n^2
M <- matrix(x, nrow = m, byrow = TRUE)

答案 3 :(得分:6)

我会创建一个稀疏矩阵:

library(Matrix)
n <- 5
inds <- (0:(n -1)) * 2 + 1
inds <- expand.grid(inds, inds)

sparseMatrix(i = inds[[2]], 
             j = inds[[1]],
             x = seq_len(n^2),
             dims = rep(2 * n - 1, 2))
#9 x 9 sparse Matrix of class "dgCMatrix"
#                            
# [1,]  1 .  2 .  3 .  4 .  5
# [2,]  . .  . .  . .  . .  .
# [3,]  6 .  7 .  8 .  9 . 10
# [4,]  . .  . .  . .  . .  .
# [5,] 11 . 12 . 13 . 14 . 15
# [6,]  . .  . .  . .  . .  .
# [7,] 16 . 17 . 18 . 19 . 20
# [8,]  . .  . .  . .  . .  .
# [9,] 21 . 22 . 23 . 24 . 25

如果需要,可以使用as.matrix将其转换为密集矩阵,但稀疏矩阵在效率方面具有一些优势。