Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# program to check if two matrices are identical
In C#, to check whether two matrices are identical, you need to compare their dimensions first and then their corresponding elements. Two matrices are considered identical if they have the same dimensions and all corresponding elements are equal.
Algorithm
The algorithm for checking matrix equality involves the following steps −
Compare the dimensions (rows and columns) of both matrices
If dimensions don't match, the matrices cannot be identical
If dimensions match, compare each corresponding element
Use a flag to track if all elements are equal
Syntax
Following is the basic syntax for matrix comparison −
if (rows1 != rows2 || cols1 != cols2) {
// Matrices cannot be compared
} else {
// Compare each element
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols1; j++) {
if (matrix1[i, j] != matrix2[i, j]) {
// Matrices are not identical
}
}
}
}
Using Predefined Matrices
Example
using System;
class MatrixComparison {
public static void Main() {
int[,] matrix1 = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
int[,] matrix2 = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
int rows1 = matrix1.GetLength(0);
int cols1 = matrix1.GetLength(1);
int rows2 = matrix2.GetLength(0);
int cols2 = matrix2.GetLength(1);
Console.WriteLine("Matrix 1:");
DisplayMatrix(matrix1, rows1, cols1);
Console.WriteLine("Matrix 2:");
DisplayMatrix(matrix2, rows2, cols2);
bool areIdentical = CheckMatrixEquality(matrix1, matrix2, rows1, cols1, rows2, cols2);
if (areIdentical) {
Console.WriteLine("The matrices are identical!");
} else {
Console.WriteLine("The matrices are not identical!");
}
}
static bool CheckMatrixEquality(int[,] mat1, int[,] mat2, int r1, int c1, int r2, int c2) {
if (r1 != r2 || c1 != c2) {
Console.WriteLine("Matrices have different dimensions and cannot be compared.");
return false;
}
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c1; j++) {
if (mat1[i, j] != mat2[i, j]) {
return false;
}
}
}
return true;
}
static void DisplayMatrix(int[,] matrix, int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
Console.Write(matrix[i, j] + " ");
}
Console.WriteLine();
}
}
}
The output of the above code is −
Matrix 1: 1 2 3 4 5 6 7 8 9 Matrix 2: 1 2 3 4 5 6 7 8 9 The matrices are identical!
Comparing Different Matrices
Example
using System;
class DifferentMatrices {
public static void Main() {
int[,] matrix1 = { {1, 2}, {3, 4} };
int[,] matrix2 = { {1, 2}, {3, 5} };
Console.WriteLine("Matrix 1:");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
Console.Write(matrix1[i, j] + " ");
}
Console.WriteLine();
}
Console.WriteLine("Matrix 2:");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
Console.Write(matrix2[i, j] + " ");
}
Console.WriteLine();
}
bool identical = true;
for (int i = 0; i < 2 && identical; i++) {
for (int j = 0; j < 2; j++) {
if (matrix1[i, j] != matrix2[i, j]) {
identical = false;
Console.WriteLine($"Difference found at position [{i},{j}]: {matrix1[i, j]} != {matrix2[i, j]}");
break;
}
}
}
Console.WriteLine(identical ? "Matrices are identical!" : "Matrices are not identical!");
}
}
The output of the above code is −
Matrix 1: 1 2 3 4 Matrix 2: 1 2 3 5 Difference found at position [1,1]: 4 != 5 Matrices are not identical!
Comparing Matrices with Different Dimensions
Example
using System;
class DifferentDimensions {
public static void Main() {
int[,] matrix1 = { {1, 2, 3}, {4, 5, 6} }; // 2x3 matrix
int[,] matrix2 = { {1, 2}, {4, 5} }; // 2x2 matrix
int rows1 = matrix1.GetLength(0);
int cols1 = matrix1.GetLength(1);
int rows2 = matrix2.GetLength(0);
int cols2 = matrix2.GetLength(1);
Console.WriteLine($"Matrix 1 dimensions: {rows1}x{cols1}");
Console.WriteLine($"Matrix 2 dimensions: {rows2}x{cols2}");
if (rows1 != rows2 || cols1 != cols2) {
Console.WriteLine("Matrices have different dimensions and cannot be identical.");
} else {
Console.WriteLine("Matrices have same dimensions. Proceeding with element comparison...");
}
}
}
The output of the above code is −
Matrix 1 dimensions: 2x3 Matrix 2 dimensions: 2x2 Matrices have different dimensions and cannot be identical.
Key Points
Dimension Check: Always verify that both matrices have the same number of rows and columns
Element Comparison: Use nested loops to compare each corresponding element
Early Exit: Use a flag or break statement to exit early when a mismatch is found
GetLength() Method: Use
GetLength(0)for rows andGetLength(1)for columns
Conclusion
Checking matrix equality in C# requires first comparing dimensions and then iterating through all elements to verify they match. Using a systematic approach with proper dimension validation ensures accurate comparison and prevents runtime errors when matrices have different sizes.
