R Basics : Create matrix of alternating 1s and 0s

Hi All,

I am pretty new to R and just started learning it. Need one help regarding the below problem.
Create the following matrix elegantly:

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

I can only come up with below option

e = matrix (c(0,1,0,1,0,1,0,1,0,1,1,0,1,0,1,0,1,0,1,0), nrow=10, ncol=10, byrow=FALSE)

Looking for help if there is any other efficient way of doing that.

Thank you.

e <- matrix(c(rep(c(0,1), 5), rep(c(1,0), 5)), nrow=10, ncol=10, byrow=FALSE)
e
#>       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#>  [1,]    0    1    0    1    0    1    0    1    0     1
#>  [2,]    1    0    1    0    1    0    1    0    1     0
#>  [3,]    0    1    0    1    0    1    0    1    0     1
#>  [4,]    1    0    1    0    1    0    1    0    1     0
#>  [5,]    0    1    0    1    0    1    0    1    0     1
#>  [6,]    1    0    1    0    1    0    1    0    1     0
#>  [7,]    0    1    0    1    0    1    0    1    0     1
#>  [8,]    1    0    1    0    1    0    1    0    1     0
#>  [9,]    0    1    0    1    0    1    0    1    0     1
#> [10,]    1    0    1    0    1    0    1    0    1     0

Created on 2019-09-06 by the reprex package (v0.3.0)

2 Likes

Hi @Abhi
The most elegant solution I've seen comes from https://www.mathworks.com/matlabcentral/answers/196631-how-can-i-create-a-matrix-of-alternating-1s-and-0s-for-any-size-matrix. From a point-of-view of comprehension, @valeri's answer is much clearer.

e  <- toeplitz(10:1%%2)
e
#>       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
#>  [1,]    0    1    0    1    0    1    0    1    0     1
#>  [2,]    1    0    1    0    1    0    1    0    1     0
#>  [3,]    0    1    0    1    0    1    0    1    0     1
#>  [4,]    1    0    1    0    1    0    1    0    1     0
#>  [5,]    0    1    0    1    0    1    0    1    0     1
#>  [6,]    1    0    1    0    1    0    1    0    1     0
#>  [7,]    0    1    0    1    0    1    0    1    0     1
#>  [8,]    1    0    1    0    1    0    1    0    1     0
#>  [9,]    0    1    0    1    0    1    0    1    0     1
#> [10,]    1    0    1    0    1    0    1    0    1     0

Created on 2019-09-06 by the reprex package (v0.3.0)

3 Likes

That is indeed very elegant :slight_smile:

Thank you so much Valeri. I am still figuring it how your solution is working and learn from it.

Thanks again and “You are awesome”

You can take advantage of recycling and do the following:

x = matrix(0:1, nrow=10, ncol=1)
x = matrix(c(x, rev(x)), nrow=10, ncol=10)
x
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    0    1    0    1    0    1    0    1    0     1
 [2,]    1    0    1    0    1    0    1    0    1     0
 [3,]    0    1    0    1    0    1    0    1    0     1
 [4,]    1    0    1    0    1    0    1    0    1     0
 [5,]    0    1    0    1    0    1    0    1    0     1
 [6,]    1    0    1    0    1    0    1    0    1     0
 [7,]    0    1    0    1    0    1    0    1    0     1
 [8,]    1    0    1    0    1    0    1    0    1     0
 [9,]    0    1    0    1    0    1    0    1    0     1
[10,]    1    0    1    0    1    0    1    0    1     0

Or, as a function:

alt.mat = function(vals, ncol, nrow) {
  x = matrix(vals, nrow, 1)
  matrix(c(x, rev(x)), nrow, ncol)
}

alt.mat(0:1, 4, 4)
alt.mat(1:4, 3, 6)
2 Likes

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.