# How to create a vector of matrices in R?

R ProgrammingServer Side ProgrammingProgramming

#### Artificial Intelligence : The Future Of Programming

15 Lectures 54 mins

#### Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

#### C Programming from scratch- Master C Programming

Best Seller

60 Lectures 8 hours

As of now it is not possible to create a vector of matrices in R. If we want to do it, we should prefer a list, hence we can create a list with matrices.

For Example, if we have matrices say M1, M2, and M3 and we want to create a list of these matrices then we can use the below given command −

list(M1,M2,M3)

## Example

To create a vector of matrices in R, use the following snippet −

List<-
list(M1=matrix(rpois(40,5),ncol=2),M2=matrix(rnorm(30),ncol=3),M3=matrix(sample(1:100,60),ncol=3))
List

The vector of matrices are as follows −

$M1 [,1] [,2] [1,] 5 6 [2,] 10 5 [3,] 3 5 [4,] 5 5 [5,] 7 4 [6,] 6 1 [7,] 5 3 [8,] 5 8 [9,] 9 7 [10,] 6 6 [11,] 3 5 [12,] 5 6 [13,] 3 1 [14,] 7 3 [15,] 5 6 [16,] 2 2 [17,] 2 3 [18,] 6 2 [19,] 2 6 [20,] 3 3$M2
[,1]       [,2]     [,3]
[1,]  1.9578909  0.45914071 -2.00944775
[2,]  0.1823403  1.05974964  0.42974335
[3,]  0.5921457  0.05078855 -0.03403513
[4,] -0.9808145  0.72762418 -1.09157212
[5,] -0.1980738 -2.32805205  0.68170467
[6,]  0.4682491 -0.48228947 -1.97650905
[7,]  0.2745822 -0.75218847  0.37580828
[8,]  1.5170457  0.02360123  0.33017557
[9,]  -0.9706965 0.95314822  0.71276779
[10,] -0.9584967 0.09087230  0.52793781
\$M3
[,1] [,2] [,3]
[1,] 10  14   22
[2,] 99  46   83
[3,] 16  60   39
[4,] 28   3   72
[5,] 95  44   79
[6,] 96  29   42
[7,] 61  59   45
[8,] 55  47   67
[9,] 25  43   75
[10,] 70  18   89
[11,] 82  48    2
[12,] 86  54   57
[13,] 21  69   12
[14,] 19 100   85
[15,] 63  32   88
[16,] 56  62   91
[17,] 41  52   33
[18,] 24  40    7
[19,] 30  17   38
[20,] 98  93   97
Updated on 10-Nov-2021 06:24:13