Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how insert zeros in seq in R

Tags:

r

seq

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 :)

like image 979
G.P Avatar asked Mar 17 '16 20:03

G.P


3 Answers

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)
like image 71
Frank Avatar answered Nov 15 '22 07:11

Frank


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
like image 44
RHertel Avatar answered Nov 15 '22 08:11

RHertel


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)
like image 6
Stibu Avatar answered Nov 15 '22 07:11

Stibu