How to Check if Two Matrices Are Identical With Programming

Two matrices are said to be identical if both of them have the same number of rows, columns, and the same corresponding elements. In this article, you’ll learn how to check if two matrices are identical using Python, C++, JavaScript, and C.

Problem Statement

You’re given two matrices mat1[][] and mat2[][]. You need to check if the two matrices are identical. If the two matrices are identical, print “Yes, the matrices are identical”. And if the two matrices aren’t identical, print “No, the matrices are not identical”.

Examples:

Condition for Two Matrices to Be Identical

Two matrices are said to be identical if and only if they satisfy the following conditions:

  1. Both matrices have the same number of rows and columns.
  2. Both matrices have the same corresponding elements.

Approach to Check if the Two Given Matrices Are Identical

You can follow the approach below to check if the two given matrices are identical or not:

  1. Run a nested loop to traverse through each element of both the matrices.
  2. If any of the corresponding elements of the two matrices are not equal, return false.
  3. And if no corresponding elements are found dissimilar ’till the end of the loop, return true.

Related: How to Add and Subtract Two Matrices in C++, Python, and JavaScript

C++ Program to Check if the Two Given Matrices Are Identical

Below is the C++ program to check if the two given matrices are identical or not:

// C++ program to check if two matrices are identical
#include
using namespace std;
// The order of the matrix is 3 x 4
#define size1 3
#define size2 4
// Function to check if two matrices are identical
bool isIdentical(int mat1[][size2], int mat2[][size2])
{
for (int i = 0; i {
for (int j = 0; j {
if (mat1[i][j] != mat2[i][j])
{
return false;
}
}
}
return true;
}
// Function to print a matrix
void printMatrix(int mat[][size2])
{
for (int i = 0; i {
for (int j = 0; j {
cout }
cout }
}
// Driver code
int main()
{
// 1st Matrix
int mat1[size1][size2] = { {2, 2, 2, 2},
{2, 2, 2, 2},
{2, 2, 2, 2} };
cout printMatrix(mat1);
// 2nd Matrix
int mat2[size1][size2] = { {2, 2, 2, 2},
{2, 2, 2, 2},
{2, 2, 2, 2} };
cout printMatrix(mat2);
if(isIdentical(mat1, mat2))
{
cout }
else
{
cout }
// 3rd Matrix
int mat3[size1][size2] = { {3, 3, 3, 3},
{3, 3, 3, 3},
{3, 3, 3, 3} };
cout printMatrix(mat3);
// 4th Matrix
int mat4[size1][size2] = { {4, 4, 4, 4},
{4, 4, 4, 4},
{4, 4, 4, 4} };
cout printMatrix(mat4);
if(isIdentical(mat3, mat4))
{
cout }
else
{
cout }
return 0;
}

Output:

Matrix 1:
2 2 2 2
2 2 2 2
2 2 2 2
Matrix 2:
2 2 2 2
2 2 2 2
2 2 2 2
Yes, the matrices are identical
Matrix 3:
3 3 3 3
3 3 3 3
3 3 3 3
Matrix 4:
4 4 4 4
4 4 4 4
4 4 4 4
No, the matrices are not identical

Related: How to Find the Sum of All Elements in an Array

Python Program to Check if the Two Given Matrices Are Identical

Below is the Python program to check if the two given matrices are identical or not:

# Python program to check if two matrices are identical
# The order of the matrix is 3 x 4
size1 = 3
size2 = 4
# Function to check if two matrices are identical
def isIdentical(mat1, mat2):
for i in range(size1):
for j in range(size2):
if (mat1[i][j] != mat2[i][j]):
return False
return True
# Function to print a matrix
def printMatrix(mat):
for i in range(size1):
for j in range(size2):
print(mat[i][j], end=' ')
print()
# Driver code
# 1st Matrix
mat1 = [ [2, 2, 2, 2],
[2, 2, 2, 2],
[2, 2, 2, 2] ]
print("Matrix 1:")
printMatrix(mat1)

# 2nd Matrix
mat2 = [ [2, 2, 2, 2],
[2, 2, 2, 2],
[2, 2, 2, 2] ]
print("Matrix 2:")
printMatrix(mat2)
if (isIdentical(mat1, mat2)):
print("Yes, the matrices are identical")
else:
print("No, the matrices are not identical")
# 3rd Matrix
mat3 = [ [3, 3, 3, 3],
[3, 3, 3, 3],
[3, 3, 3, 3] ]
print("Matrix 3:")
printMatrix(mat3)

# 4th Matrix
mat4 = [ [4, 4, 4, 4],
[4, 4, 4, 4],
[4, 4, 4, 4] ]
print("Matrix 4:")
printMatrix(mat4)
if (isIdentical(mat3, mat4)):
print("Yes, the matrices are identical")
else:
print("No, the matrices are not identical")

Output:

Matrix 1:
2 2 2 2
2 2 2 2
2 2 2 2
Matrix 2:
2 2 2 2
2 2 2 2
2 2 2 2
Yes, the matrices are identical
Matrix 3:
3 3 3 3
3 3 3 3
3 3 3 3
Matrix 4:
4 4 4 4
4 4 4 4
4 4 4 4
No, the matrices are not identical

Related: How to Create and Use Tuples in Python

JavaScript Program to Check if the Two Given Matrices Are Identical

Below is the JavaScript program to check if the two given matrices are identical or not:

// JavaScript program to check if two matrices are identical
// The order of the matrix is 3 x 4
var size1 = 3;
var size2 = 4;
// Function to check if two matrices are identical
function isIdentical(mat1, mat2) {
for (let i = 0; i {
for (let j = 0; j {
if (mat1[i][j] != mat2[i][j])
{
return false;
}
}
}
return true;
}
// Function to print a matrix
function printMatrix(mat) {
for (let i = 0; i for (let j = 0; j document.write(mat[i][j] + " ");
}
document.write("
");
}
}
// Driver code
// 1st Matrix
var mat1 = [ [2, 2, 2, 2],
[2, 2, 2, 2],
[2, 2, 2, 2] ];
document.write("Matrix 1:" + "
");
printMatrix(mat1);

// 2nd Matrix
var mat2 = [ [2, 2, 2, 2],
[2, 2, 2, 2],
[2, 2, 2, 2] ];
document.write("Matrix 2:" + "
");
printMatrix(mat2);
if (isIdentical(mat1, mat2)) {
document.write("Yes, the matrices are identical" + "
");
} else{
document.write("No, the matrices are not identical" + "
");
}
// 3rd Matrix
var mat3 = [ [3, 3, 3, 3],
[3, 3, 3, 3],
[3, 3, 3, 3] ];
document.write("Matrix 3:" + "
");
printMatrix(mat3);
// 4th Matrix
var mat4 = [ [4, 4, 4, 4],
[4, 4, 4, 4],
[4, 4, 4, 4] ];
document.write("Matrix 4:" + "
");
printMatrix(mat4);
if (isIdentical(mat3, mat4)) {
document.write("Yes, the matrices are identical" + "
");
} else{
document.write("No, the matrices are not identical" + "
");
}

Output:

Matrix 1:
2 2 2 2
2 2 2 2
2 2 2 2
Matrix 2:
2 2 2 2
2 2 2 2
2 2 2 2
Yes, the matrices are identical
Matrix 3:
3 3 3 3
3 3 3 3
3 3 3 3
Matrix 4:
4 4 4 4
4 4 4 4
4 4 4 4
No, the matrices are not identical

Related: Create a CAPTCHA Validation Form Using HTML, CSS, and JavaScript

C Program to Check if the Two Given Matrices Are Identical

Below is the C program to check if the two given matrices are identical or not:

// C program to check if two matrices are identical
#include
#include
// The order of the matrix is 3 x 4
#define size1 3
#define size2 4
// Function to check if two matrices are identical
bool isIdentical(int mat1[][size2], int mat2[][size2])
{
for (int i = 0; i {
for (int j = 0; j {
if (mat1[i][j] != mat2[i][j])
{
return false;
}
}
}
return true;
}
// Function to print a matrix
void printMatrix(int mat[][size2])
{
for (int i = 0; i {
for (int j = 0; j {
printf("%d ", mat[i][j]);
}
printf("⁠n");
}
}
// Driver code
int main()
{
// 1st Matrix
int mat1[size1][size2] = { {2, 2, 2, 2},
{2, 2, 2, 2},
{2, 2, 2, 2} };
printf("Matrix 1:⁠n");
printMatrix(mat1);
// 2nd Matrix
int mat2[size1][size2] = { {2, 2, 2, 2},
{2, 2, 2, 2},
{2, 2, 2, 2} };
printf("Matrix 2:⁠n");
printMatrix(mat2);
if(isIdentical(mat1, mat2))
{
printf("Yes, the matrices are identical ⁠n");
}
else
{
printf("No, the matrices are not identical ⁠n");
}
// 3rd Matrix
int mat3[size1][size2] = { {3, 3, 3, 3},
{3, 3, 3, 3},
{3, 3, 3, 3} };
printf("Matrix 3: ⁠n");
printMatrix(mat3);
// 4th Matrix
int mat4[size1][size2] = { {4, 4, 4, 4},
{4, 4, 4, 4},
{4, 4, 4, 4} };
printf("Matrix 4: ⁠n");
printMatrix(mat4);
if(isIdentical(mat3, mat4))
{
printf("Yes, the matrices are identical ⁠n");
}
else
{
printf("No, the matrices are not identical ⁠n");
}
return 0;
}

Output:

Matrix 1:
2 2 2 2
2 2 2 2
2 2 2 2
Matrix 2:
2 2 2 2
2 2 2 2
2 2 2 2
Yes, the matrices are identical
Matrix 3:
3 3 3 3
3 3 3 3
3 3 3 3
Matrix 4:
4 4 4 4
4 4 4 4
4 4 4 4
No, the matrices are not identical

Learn a New Programming Language

Computer Science is expanding at a very fast rate, and the competition in this field is more intense than ever. You must keep yourself updated with the latest skills and programming languages. Whether you’re a beginner or an experienced programmer, in any case, you should learn some of the programming languages according to industry requirements.

Source: makeuseof.com

Related posts

Best Tablets for Kids in 2024

What Is a Snapchat Public Profile and How Do You Create One?

Connections #343: Today’s Answer and Clues (Sunday, May 19, 2024)