# Find the intersection of two Matrices

Given two matrix A[][] and B[][] of same order m*n. The task is to find the intersection of both matrix as C, where:

• Cij = Aij if Aij = Bij
• C = “*”, otherwise.

Examples:

```Input :
A[N][N] = {{2, 4, 6, 8},
{1, 3, 5, 7},
{8, 6, 4, 2},
{7, 5, 3, 1}};

B[N][N] = {{0, 4, 3, 8},
{1, 3, 5, 7},
{8, 3, 6, 2},
{4, 5, 3, 4}};
Output :
* 4 * 8
1 3 5 7
8 * * 2
* 5 3 *
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

As intersection of two set is a set which includes common elements to both set, similarly intersection of two matrix will include only corresponding common element and place “*” at the position of rest unmatching elements.

For finding the intersection of both matrix simply iterate over their size and print * if element at particular position in both matrix are not equal else print the element.

Below is the implementation of the above approach:

 `// CPP program to find intersection ` `// of two matrices ` ` `  `#include ` `#define N 4 ` `#define M 4 ` ` `  `using` `namespace` `std; ` ` `  `// Function to print the resultant matrix ` `void` `printIntersection(``int` `A[][N], ``int` `B[][N]) ` `{ ` `    ``for` `(``int` `i = 0; i < M; i++) { ` `        ``for` `(``int` `j = 0; j < N; j++) { ` ` `  `            ``// print element value for equal ` `            ``// elements else * ` `            ``if` `(A[i][j] == B[i][j]) ` `                ``cout << A[i][j] << ``" "``; ` `            ``else` `                ``cout << ``"* "``; ` `        ``} ` ` `  `        ``cout << ``"\n"``; ` `    ``} ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `A[M][N] = { { 2, 4, 6, 8 }, ` `                    ``{ 1, 3, 5, 7 }, ` `                    ``{ 8, 6, 4, 2 }, ` `                    ``{ 7, 5, 3, 1 } }; ` `    ``int` `B[M][N] = { { 2, 3, 6, 8 }, ` `                    ``{ 1, 3, 5, 2 }, ` `                    ``{ 8, 1, 4, 2 }, ` `                    ``{ 3, 5, 4, 1 } }; ` ` `  `    ``printIntersection(A, B); ` ` `  `    ``return` `0; ` `} `

 `//  Java program to find intersection ` `// of two matrices ` ` `  `import` `java.io.*; ` ` `  `class` `GFG { ` `  ``static` `int` `N = ``4``; ` `static` `int` `M = ``4``; ` ` `  ` `  ` `  `// Function to print the resultant matrix ` `static` `void` `printIntersection(``int` `A[][], ``int` `B[][]) ` `{ ` `    ``for` `(``int` `i = ``0``; i < M; i++) { ` `        ``for` `(``int` `j = ``0``; j < N; j++) { ` ` `  `            ``// print element value for equal ` `            ``// elements else * ` `            ``if` `(A[i][j] == B[i][j]) ` `                ``System.out.print(A[i][j] +``" "``); ` `            ``else` `                ``System.out.print( ``"* "``); ` `        ``} ` ` `  `        ``System.out.println( ``" "``); ` `    ``} ` `} ` ` `  `// Driver Code ` ` `  `    ``public` `static` `void` `main (String[] args) { ` `           ``int` `A[][] = { { ``2``, ``4``, ``6``, ``8` `}, ` `                    ``{ ``1``, ``3``, ``5``, ``7` `}, ` `                    ``{ ``8``, ``6``, ``4``, ``2` `}, ` `                    ``{ ``7``, ``5``, ``3``, ``1` `} }; ` `    ``int` `B[][] = { { ``2``, ``3``, ``6``, ``8` `}, ` `                    ``{ ``1``, ``3``, ``5``, ``2` `}, ` `                    ``{ ``8``, ``1``, ``4``, ``2` `}, ` `                    ``{ ``3``, ``5``, ``4``, ``1` `} }; ` ` `  `    ``printIntersection(A, B); ` ` `  `    ``} ` `} ` `// This code is contributed by anuj_67.. `

 `# Python 3 program to find intersection ` `# of two matrices ` `N, M ``=` `4``, ``4` ` `  `# Function to print the resultant matrix ` `def` `printIntersection(A, B) : ` ` `  `    ``for` `i ``in` `range``(M) : ` `        ``for` `j ``in` `range``(N) : ` ` `  `            ``# print element value for equal ` `            ``# elements else * ` `            ``if` `(A[i][j] ``=``=` `B[i][j]) : ` `                ``print``(A[i][j], end ``=` `" "``) ` `            ``else` `: ` `                ``print``(``"* "``, end ``=` `" "``) ` `         `  `        ``print``() ` `         `  `# Driver Code ` `if` `__name__ ``=``=` `"__main__"` `: ` `     `  `    ``A ``=` `[ [``2``, ``4``, ``6``, ``8` `], ` `        ``[ ``1``, ``3``, ``5``, ``7` `], ` `        ``[ ``8``, ``6``, ``4``, ``2` `], ` `        ``[ ``7``, ``5``, ``3``, ``1` `] ] ` `         `  `    ``B ``=` `[ [ ``2``, ``3``, ``6``, ``8` `], ` `        ``[ ``1``, ``3``, ``5``, ``2` `], ` `        ``[ ``8``, ``1``, ``4``, ``2` `], ` `        ``[ ``3``, ``5``, ``4``, ``1` `] ] ` ` `  `    ``printIntersection(A, B) ` ` `  `# This code is contributed by Ryuga `

 `// C# program to find intersection ` `// of two matrices ` ` `  `using` `System; ` ` `  `class` `GFG { ` `static` `int` `N = 4; ` `static` `int` `M = 4; ` ` `  ` `  ` `  `// Function to print the resultant matrix ` `static` `void` `printIntersection(``int` `[,]A, ``int` `[,]B) ` `{ ` `    ``for` `(``int` `i = 0; i < M; i++) { ` `        ``for` `(``int` `j = 0; j < N; j++) { ` ` `  `            ``// print element value for equal ` `            ``// elements else * ` `            ``if` `(A[i,j] == B[i,j]) ` `                ``Console.Write(A[i,j] +``" "``); ` `            ``else` `                ``Console.Write( ``"* "``); ` `        ``} ` ` `  `        ``Console.WriteLine( ``" "``); ` `    ``} ` `} ` ` `  `// Driver Code ` ` `  `    ``public` `static` `void` `Main () { ` `        ``int` `[,]A = { { 2, 4, 6, 8 }, ` `                    ``{ 1, 3, 5, 7 }, ` `                    ``{ 8, 6, 4, 2 }, ` `                    ``{ 7, 5, 3, 1 } }; ` `    ``int` `[,]B = { { 2, 3, 6, 8 }, ` `                    ``{ 1, 3, 5, 2 }, ` `                    ``{ 8, 1, 4, 2 }, ` `                    ``{ 3, 5, 4, 1 } }; ` ` `  `    ``printIntersection(A, B); ` ` `  `    ``} ` `} ` `// This code is contributed by inder_verma.. `

 ` `

Output:
```2 * 6 8
1 3 5 *
8 * 4 2
* 5 * 1
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

Discovering ways to develop a plane for soaring career goals

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :