Home > Blockchain >  merging two matrices in particular sequence
merging two matrices in particular sequence

Time:01-06

I have two matrices suppose A of order 150*20 and B of order 70*20. I want to merge matrix B in A in such a manner that rows

1, 2, 3, ...,70 

of B are merged in such a manner in A that the above rows of B are rows

1,7,8,14,15,21,22,28,29...

of A. Can anyone help to merge matrix B in A, such that the order of A becomes 220*20

CodePudding user response:

A <- matrix((1:30)*10, 30, 3)
B <- matrix(1:9, 9, 3)
y <- c(1, outer(0:1,  7*1:4, ' ')) # Your vector of order B.
mat <- matrix(NA, nrow(A) nrow(B), ncol(A))

mat[y, ] <- B
mat[-y, ] <- A
mat

    [,1] [,2] [,3]
 [1,]    1    1    1
 [2,]   10   10   10
 [3,]   20   20   20
 [4,]   30   30   30
 [5,]   40   40   40
 [6,]   50   50   50
 [7,]    2    2    2
 [8,]    3    3    3
 [9,]   60   60   60
[10,]   70   70   70
[11,]   80   80   80
[12,]   90   90   90
[13,]  100  100  100
[14,]    4    4    4
[15,]    5    5    5
[16,]  110  110  110
[17,]  120  120  120
[18,]  130  130  130
[19,]  140  140  140
[20,]  150  150  150
[21,]    6    6    6
[22,]    7    7    7
[23,]  160  160  160
[24,]  170  170  170
[25,]  180  180  180
[26,]  190  190  190
[27,]  200  200  200
[28,]    8    8    8
[29,]    9    9    9
[30,]  210  210  210
[31,]  220  220  220
[32,]  230  230  230
[33,]  240  240  240
[34,]  250  250  250
[35,]  260  260  260
[36,]  270  270  270
[37,]  280  280  280
[38,]  290  290  290
[39,]  300  300  300
  •  Tags:  
  • Related