# [R] create new matrices with specific patterns

Kathryn Lord kathryn.lord2000 at gmail.com
Tue Jan 27 03:47:04 CET 2015

```Dear R users,

Suppose I have a matrix A.

> p <- 1:4
> q <- 1:5
> P<-rep(p, each=5)
> Q<-rep(q, 4)
>
> A <- cbind(P,Q)
> A
P Q
[1,] 1 1
[2,] 1 2
[3,] 1 3
[4,] 1 4
[5,] 1 5
[6,] 2 1
[7,] 2 2
[8,] 2 3
[9,] 2 4
[10,] 2 5
[11,] 3 1
[12,] 3 2
[13,] 3 3
[14,] 3 4
[15,] 3 5
[16,] 4 1
[17,] 4 2
[18,] 4 3
[19,] 4 4
[20,] 4 5
>

With the matrix A, I'd like to generate new matrices B, ..., E below.

B = A[(3,4), (1,2)]
C = A[(2,2), (1,5), (1,1)]
D = A[(4,2)]
E = A[(3,0), (1,4)]

Matrix B means that first three 'p's (1,2,3) has four 'q's (1,2,3,4) and
the forth 'p' element (4) has two 'q's (1,2); in other words,

Is there the easyiest way to create B,...,E in R?

Actually, the example above is a toy example and the matrix A I have is
around 10,000 by 10,000 and the pattern is also very complicated.

Any suggestion will be greatly appreciated.

Best,

Kathryn Lord

[[alternative HTML version deleted]]

```