Home > Mobile >  Merging two matrices in a specific sequence
Merging two matrices in a specific sequence

Time:01-19

I have a matrix A of order 150*24 and a second matrix B of order 50*24. I want to merge the matrix B in matrix A in such a way that row of matrix B are

[1,2,3,4,5,...,50]

merge in A in such a manner that the position of the matrix B in matrix A become

[5,6,12,13,19,20,26,27,33,34,40,41,47,48,54,55,61,62,68,69,75,76,82,83,...]

so the matrix A become of order 200*24, how could I do this in R?

CodePudding user response:

Here's a way to do it. First, create the vector:

n = 150/7
s <- sequence(nvec = rep(2, n), from = (7*1:n)-2)
 [1]   5   6  12  13  19  20  26  27  33  34  40  41  47  48  54  55  61  62  68  69  75  76  82  83  89  90  96  97
[29] 103 104 110 111 117 118 124 125 131 132 138 139 145 146

Then, to add multiple rows to specific position, you can do:

for (i in seq(s)){
  A <- rbind(A[1:(s[i]-1),], B[i,], A[-(1:(s[i]-1)),])
}

data

A = array(c(1,0), dim = c(150,24))
B = array(c(1,0), dim = c(50,24))
  •  Tags:  
  • Related