How to create a matrix with vectors as elements in R?


To create a matrix with vectors as elements in R, we can create arrays because an array contains matrices with vectors as elements.

Check out the below given examples of arrays and vectors extracted by the arrays to understand how matrices stored in an array represent vectors as elements.

Example 1

Following snippet creates arrays consisting of matrices −

Array<-array(1:100,c(5,2,5))
Array

The following arrays are created −

, , 1

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

, , 2

    [,1][,2]
[1,] 11  16
[2,] 12  17
[3,] 13  18
[4,] 14  19
[5,] 15  20

, , 3

     [,1][,2]
[1,]  21  26
[2,]  22  27
[3,]  23  28
[4,]  24  29
[5,]  25  30

, , 4

    [,1][,2]
[1,] 31  36
[2,] 32  37
[3,] 33  38
[4,] 34  39
[5,] 35  40

, , 5

    [,1][,2]
[1,] 41  46
[2,] 42  47
[3,] 43  48
[4,] 44  49
[5,] 45  50

To extract the vectors from matrices in array, add the following code to the above snippet −

Array<-array(1:100,c(5,2,5))
Array[1,2,]

Output

If you execute all the above given snippets as a single program, it generates the following output −

[1] 6 16 26 36 46

To extract the vectors from matrices in array, add the following code to the above snippet −

Array<-array(1:100,c(5,2,5))
Array[5,2,]

Output

If you execute all the above given snippets as a single program, it generates the following output −

[1] 10 20 30 40 50

To extract the vectors from matrices in array, add the following code to the above snippet −

Array<-array(1:100,c(5,2,5))
Array[5,1,]

Output

If you execute all the above given snippets as a single program, it generates the following output −

[1] 5 15 25 35 45

Example 2

Following snippet creates arrays consisting of matrices −

Array_dat<-array(rpois(100,10),c(5,5,4))
Array_dat

The following arrays are created −

, , 1

     [,1][,2][,3][,4][,5]
[1,]  10  13   7  10  16
[2,]   6  11   6  12   8
[3,]   5   6  10   8  17
[4,]   7   9  13   4  11
[5,]  14  12   6  11   6

, , 2

     [,1][,2][,3][,4][,5]
[1,] 12  10   12  14  16
[2,]  9   9   14  16  12
[3,]  5   8    8  14  11
[4,]  6  10   11   8  17
[5,] 10  11   11  13   9

, , 3

     [,1][,2][,3][,4][,5]
[1,]  10  12   8   6  10
[2,]   9  11   5  13  12
[3,]  12  10   5   8  11
[4,]   9  13  10  12  13
[5,]  16   8  13  14  11

, , 4

     [,1][,2][,3][,4][,5]
[1,]  11  8   10  6   13
[2,]  14  8   10  8   12
[3,]   8  8    9  6    6
[4,]  12  9    4  9   16
[5,]   9  8   11 18   11

To extract vectors from matrices in Array_dat, add the following code to the above snippet −

Array_dat<-array(rpois(100,10),c(5,5,4))
Array_dat[1,1,]

Output

If you execute all the above given snippets as a single program, it generates the following output −

[1] 10 12 10 11

To extract vectors from matrices in Array_dat, add the following code to the above snippet −

Array_dat<-array(rpois(100,10),c(5,5,4))
Array_dat[1,2,]

Output

If you execute all the above given snippets as a single program, it generates the following output −

[1] 13 10 12 8

Updated on: 23-Nov-2021

564 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements