how insert zeros in seq in R
Asked Answered
L

4

11

I need get this:

      [,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

I write:

  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

So, can I get this using n ? for example: x <- 1:n^2 ?? That is, I want to have seq(n^2) in a matrix, but I need rows with zeros and columns with zeros?

I hope my question is understandable, thank you very much :)

Lobster answered 17/3, 2016 at 20:0 Comment(0)
J
16

Starting from n...

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

It's not the fastest way, I'm sure, but it does work:

      [,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

Alternately, to avoid transposing, use 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)
Jena answered 17/3, 2016 at 20:10 Comment(3)
Wow, that is nice. Looks like you are inching to 10 wtihin another 5 mins.Naturalist
nice! but I think a typo: the col(m) should be col(tm) and similarly for row(m)Laundress
Nice answer @JenaManes
P
10

You can use a Kronecker product for this:

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
Patriciapatrician answered 17/3, 2016 at 20:27 Comment(3)
I like that one. I had never heard about the Kronecker product... You can actually remove the last row and column in one go: m3[-nrow(m3), -ncol(m3)].Privacy
Right. Thank you @Privacy !Patriciapatrician
code-golfed version (t(matrix(1:n^2, n)) %x% matrix(c(1,0,0,0),2))[-2*n,-2*n]Manes
P
6

This solution first creates the vector x as in the OP's code and then converts to a matrix:

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)
Privacy answered 17/3, 2016 at 20:25 Comment(1)
rep_len? interesting shiny new function thereUpstage
L
6

I would create a sparse matrix:

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

This can be transformed into a dense matrix using as.matrix if needed, but sparse matrices offer some advantages regarding efficiency.

Lura answered 18/3, 2016 at 7:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.