# How to check in R whether a matrix element is present in another matrix or not?

We can use %in% to check whether a matrix element is present in another matrix or not. For example, suppose we have two matrices defined as −

   M1
1 2 3
1 2 3
1 2 3
M2
1 2 3
4 5 6
7 8 9

Then M1%in%M2 will return −

[1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE

But M2%in%M1 will return −

[1] TRUE FALSE FALSE TRUE FALSE FALSE TRUE FALSE FALSE

## Example1

Live Demo

M1<−matrix(1:25,ncol=5,byrow=TRUE)
M1
[,1] [,2] [,3] [,4] [,5]
[1,] 1 2 3 4 5
[2,] 6 7 8 9 10
[3,] 11 12 13 14 15
[4,] 16 17 18 19 20
[5,] 21 22 23 24 25
M2<−matrix(1:50,ncol=5,byrow=TRUE)
M2
[,1] [,2] [,3] [,4] [,5]
[1,] 1 2 3 4 5
[2,] 6 7 8 9 10
[3,] 11 12 13 14 15
[4,] 16 17 18 19 20
[5,] 21 22 23 24 25
[6,] 26 27 28 29 30
[7,] 31 32 33 34 35
[8,] 36 37 38 39 40
[9,] 41 42 43 44 45
[10,] 46 47 48 49 50
M1%in%M2
[1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[16] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
M2%in%M1
[1] TRUE TRUE TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE TRUE TRUE
[13] TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE
[25] TRUE FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE TRUE FALSE
[37] FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE TRUE FALSE FALSE FALSE
[49] FALSE FALSE

## Example2

Live Demo

M3<−matrix(rpois(40,5),ncol=4,byrow=TRUE)
M3
[,1] [,2] [,3] [,4]
[1,] 4 7 7 6
[2,] 5 4 5 2
[3,] 6 3 5 5
[4,] 8 6 6 6
[5,] 3 6 3 6
[6,] 5 3 7 8
[7,] 9 4 6 2
[8,] 4 6 2 4
[9,] 3 2 4 7
[10,] 6 10 3 2
M4<−matrix(rpois(24,5),ncol=4,byrow=TRUE)
M4
[,1] [,2] [,3] [,4]
[1,] 4 8 7 5
[2,] 9 3 8 9
[3,] 6 2 1 5
[4,] 2 10 7 1
[5,] 5 4 7 7
[6,] 4 5 12 6
M3%in%M4
[1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[16] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[31] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
M4%in%M3
[1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[13] TRUE TRUE FALSE TRUE TRUE FALSE TRUE TRUE TRUE FALSE TRUE TRUE

## Example3

Live Demo

M6<−matrix(rpois(35,6),ncol=7,byrow=TRUE)
M6
[,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,] 6 5 9 5 2 4 7
[2,] 7 5 8 8 8 7 5
[3,] 10 4 6 9 8 10 4
[4,] 7 8 9 5 7 6 8
[5,] 6 3 5 8 9 7 6
M7<−matrix(rpois(63,8),ncol=7,byrow=TRUE)
M7
[,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,] 7 10 7 10 12 7 8
[2,] 6 7 6 6 7 10 9
[3,] 12 11 7 8 12 6 6
[4,] 6 6 8 13 6 10 5
[5,] 7 7 11 12 10 3 8
[6,] 14 3 11 10 10 3 8
[7,] 9 12 6 8 4 13 7
[8,] 8 10 12 3 4 4 7
[9,] 8 9 10 9 7 7 9
M6%in%M7
[1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[13] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE FALSE TRUE TRUE TRUE
[25] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
M7%in%M6
[1] TRUE TRUE FALSE TRUE TRUE FALSE TRUE TRUE TRUE TRUE TRUE FALSE
[13] TRUE TRUE TRUE FALSE TRUE TRUE TRUE TRUE TRUE TRUE FALSE FALSE
[25] TRUE FALSE TRUE TRUE TRUE TRUE FALSE FALSE TRUE TRUE TRUE TRUE
[37] FALSE TRUE FALSE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[49] TRUE TRUE TRUE FALSE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[61] TRUE TRUE TRUE

Updated on: 06-Nov-2020

213 Views

##### Kickstart Your Career

Get certified by completing the course

Advertisements