# How to find the maximum value in each matrix stored in an R list?

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

To find the maximum value in each matrix 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 lapply function to find the maximum of each matrix.

## 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(rpois(20,5),ncol=2)
M2<-matrix(rpois(20,5),ncol=2)
M3<-matrix(rpois(20,5),ncol=2)
M4<-matrix(rpois(20,5),ncol=2)
M5<-matrix(rpois(20,5),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,] 1 6
[2,] 8 8
[3,] 3 8
[4,] 2 9
[5,] 8 5
[6,] 7 4
[7,] 4 5
[8,] 2 3
[9,] 6 6
[10,] 2 5

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

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

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

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

## Find the maximum of each matrix

Using max function along with lapply function to find the maximum of each matrix stored in List −

Live Demo

M1<-matrix(rpois(20,5),ncol=2)
M2<-matrix(rpois(20,5),ncol=2)
M3<-matrix(rpois(20,5),ncol=2)
M4<-matrix(rpois(20,5),ncol=2)
M5<-matrix(rpois(20,5),ncol=2)
List<-list(M1,M2,M3,M4,M5)
lapply(List,FUN=max)

### Output

[]
 9

[]
 10

[]
 7

[]
 10

[]
 10