# How to find unique matrices in a list in R?

A list can contain many types of elements such as vectors, matrices, data frames etc. If we have matrices in a list then to find unique matrices in that list, we can simply use unique function. For example, if we have a list called LIST that contains matrices having some duplicate matrices then unique matrices can be extracted by using unique(LIST).

## Example1

Live Demo

list(M1=matrix(1:25,ncol=5),M2=matrix(1:25,ncol=5),M3=matrix(1:25,ncol=5),M4=matrix(rpois(25,5),ncol=5))
List1

## Output

$M1 [,1] [,2] [,3] [,4] [,5] [1,] 1 6 11 16 21 [2,] 2 7 12 17 22 [3,] 3 8 13 18 23 [4,] 4 9 14 19 24 [5,] 5 10 15 20 25$M2
[,1] [,2] [,3] [,4] [,5]
[1,] 1 6 11 16 21
[2,] 2 7 12 17 22
[3,] 3 8 13 18 23
[4,] 4 9 14 19 24
[5,] 5 10 15 20 25
$M3 [,1] [,2] [,3] [,4] [,5] [1,] 1 6 11 16 21 [2,] 2 7 12 17 22 [3,] 3 8 13 18 23 [4,] 4 9 14 19 24 [5,] 5 10 15 20 25$M4
[,1] [,2] [,3] [,4] [,5]
[1,] 2 3 4 6 5
[2,] 6 5 2 5 8
[3,] 3 5 5 6 6
[4,] 6 3 4 4 3
[5,] 6 10 4 6 7

## Example

unique(List1)

## Output

[[1]]
[,1] [,2] [,3] [,4] [,5]
[1,] 1 6 11 16 21
[2,] 2 7 12 17 22
[3,] 3 8 13 18 23
[4,] 4 9 14 19 24
[5,] 5 10 15 20 25
[[2]]
[,1] [,2] [,3] [,4] [,5]
[1,] 2 3 4 6 5
[2,] 6 5 2 5 8
[3,] 3 5 5 6 6
[4,] 6 3 4 4 3
[5,] 6 10 4 6 7

## Example2

Live Demo

list(M1=matrix(rpois(25,5),ncol=5),M2=matrix(rpois(25,2),ncol=5),M3=matrix(rpois(25,2),ncol=5),M4=matrix(rpois(25,5),ncol=5))
List2

## Output

$M1 [,1] [,2] [,3] [,4] [,5] [1,] 3 6 4 6 5 [2,] 4 7 2 9 5 [3,] 7 8 8 6 2 [4,] 3 9 5 7 7 [5,] 11 7 8 5 3$M2
[,1] [,2] [,3] [,4] [,5]
[1,] 3 4 3 3 1
[2,] 3 3 1 2 3
[3,] 0 0 0 0 1
[4,] 2 6 2 2 1
[5,] 2 0 5 2 0
$M3 [,1] [,2] [,3] [,4] [,5] [1,] 1 3 4 1 1 [2,] 3 1 3 1 1 [3,] 3 1 3 0 1 [4,] 0 1 1 3 4 [5,] 2 2 1 0 0$M4
[,1] [,2] [,3] [,4] [,5]
[1,] 6 4 4 4 6
[2,] 7 4 2 4 4
[3,] 5 3 5 9 5
[4,] 6 2 7 2 6
[5,] 8 3 5 5 1

## Example

unique(List2)

## Output

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