How to check if two matrices are Identical

Overview

Two given matrices are identical if they have the same number of rows, columns, and the same related elements. We use the isIdentical() function for that. One more condition for the matrix to be identical is that it must be square.

A and B are identical

Example

#include <iostream>
using namespace std;
#define size 3
// Function to check matrix are indentical or not
bool isIdentical(int MatrixA[][size], int MatrixB[][size])
{
for (int row = 0; row < size; row++)
{
for (int column = 0; column < size; column++)
{
if (MatrixA[row][column] != MatrixB[row][column])
{
return false;
}
}
}
return true;
}
int main()
{
// declare two matrix MatrixA and MatrixB
int MatrixA[size][size] = { {1, 1, 1},
{2, 2, 2},
{3, 3, 3}};
int MatrixB[size][size] = { {1, 1, 1},
{2, 2, 2},
{3, 3, 3}};
// call the function isIdentical to check matrix are indentical or not
if (isIdentical(MatrixA, MatrixB))
{
cout << "****** Matrices are identical ******"<<endl;
}
else
{
cout << "****** Matrices are not identical *****"<<endl;
}
return 0;
}

Explanation

  • Line 6–19: We implement the isIdentical() function.

  • Line 23–30: We declare two matrices MatrixA and MatrixB.

  • Line 32: We call the function isIdentical() to check whether MatrixA and MatrixB are identical or not.

Free Resources