Home > Software design >  How to create a matrix with four 1's in each row staggered
How to create a matrix with four 1's in each row staggered

Time:12-05

How can I easily create this matrix using clever commands:

1 0 0
1 0 0
1 0 0
1 0 0
0 1 0 
0 1 0
0 1 0 
0 1 0 
0 0 1
0 0 1
0 0 1
0 0 1

CodePudding user response:

unname(model.matrix(~gl(3,4)   0))

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

Another Option:

as.matrix(Matrix::bdiag(rep(list(rep(1,4)),3)))

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

as.matrix(Matrix::bdiag(replicate(3, numeric(4) 1, FALSE)))

CodePudding user response:

matrix(rep(c(1,0,0,0,1,0,0,0,1), each = 4),ncol = 3)

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

CodePudding user response:

#To create the matrix you have described, you can use the repmat function in MATLAB or Octave. This function creates a matrix by repeating the elements of another matrix.

#For example, to create the matrix you have described, you could do the following:

>> A = [1 0 0; 1 0 0; 1 0 0; 1 0 0]
A =
     1     0     0
     1     0     0
     1     0     0
     1     0     0

>> B = [0 1 0; 0 1 0; 0 1 0; 0 1 0]
B =
     0     1     0
     0     1     0
     0     1     0
     0     1     0

>> C = [0 0 1; 0 0 1; 0 0 1; 0 0 1]
C =
     0     0     1
     0     0     1
     0     0     1
     0     0     1

>> D = [A;B;C]
D =
     1     0     0
     1     0     0
     1     0     0
     1     0     0
     0     1     0
     0     1     0
     0     1     0
     0     1     0
     0     0     1
     0     0     1
     0     0     1
     0     0     1
#Alternatively, you could use the repmat function to create the matrix more concisely, like this:

>> E = repmat([1 0 0; 0 1 0; 0 0 1], 4, 1)
E =
     1     0     0
     1     0     0
     1     0     0
     1     0     0
     0     1     0
     0     1     0
     0     1     0
     0     1     0
     0     0     1
     0     0     1
     0     0     1
     0     0     1

#Note that the repmat function takes two arguments: the matrix to be repeated, and the number of times to repeat it in each dimension. In the example above, we have repeated the matrix [1 0 0; 0 1 0; 0 0 1] four times in the first dimension (i.e. along the rows), and once in the second dimension (i.e. along the columns). This creates the desired matrix.



  • Related