# How to find the maximum value of all matrices stored in an R list?

R ProgrammingServer Side ProgrammingProgramming

To find the maximum value of all matrices stored in an R list, we can follow the below steps −

• First of all, create a list of matrices.
• Then, use max function along with unlist and lapply function to find the maximum of all matrices.

## Create the list of matrices

Using matrix function to create multiple matrices and stored them in a list using list function −

Live Demo

M1<-matrix(sample(1:100,20),ncol=2)
M2<-matrix(sample(1:100,20),ncol=2)
M3<-matrix(sample(1:100,20),ncol=2)
M4<-matrix(sample(1:100,20),ncol=2)
M5<-matrix(sample(1:100,20),ncol=2)
List<-list(M1,M2,M3,M4,M5)
List

On executing, the above script generates the below output(this output will vary on your system due to randomization) −

[]
[,1] [,2]
[1,] 40 84
[2,] 4 48
[3,] 56 30
[4,] 9 46
[5,] 54 47
[6,] 16 88
[7,] 80 100
[8,] 32 23
[9,] 41 76
[10,] 79 52

[]
[,1] [,2]
[1,] 59 82
[2,] 39 87
[3,] 49 48
[4,] 2 18
[5,] 19 47
[6,] 72 90
[7,] 3 29
[8,] 43 9
[9,] 45 76
[10,] 65 28

[]
[,1] [,2]
[1,] 84 20
[2,] 42 95
[3,] 22 44
[4,] 34 52
[5,] 65 25
[6,] 1 92
[7,] 41 13
[8,] 68 97
[9,] 64 27
[10,] 50 6

[]
[,1] [,2]
[1,] 72 29
[2,] 13 41
[3,] 36 89
[4,] 42 30
[5,] 68 3
[6,] 94 60
[7,] 70 44
[8,] 80 26
[9,] 10 84
[10,] 35 73

[]
[,1] [,2]
[1,] 24 80
[2,] 75 18
[3,] 36 100
[4,] 69 51
[5,] 17 14
[6,] 77 6
[7,] 2 37
[8,] 96 63
[9,] 30 90
[10,] 86 47

## Find the maximum of all matrices

Using max function along with unlist and lapply function to find the maximum of all matrices stored in List −

Live Demo

M1<-matrix(sample(1:100,20),ncol=2)
M2<-matrix(sample(1:100,20),ncol=2)
M3<-matrix(sample(1:100,20),ncol=2)
M4<-matrix(sample(1:100,20),ncol=2)
M5<-matrix(sample(1:100,20),ncol=2)
List<-list(M1,M2,M3,M4,M5)
max(unlist(lapply(List,FUN=max)))

### Output

 100