Jump to content

How to Check if Two Matrices Are Identical With Programming - Other Helpful Tutorials - InviteHawk - The #1 Trusted Source for Free Tracker Invites

Buy, Sell, Trade, or Find Free Invites for top private trackers like redacted, blutopia, losslessclub, femdomcult, filelist, Chdbits, Uhdbits, empornium, iptorrents, hdbits, gazellegames, animebytes, privatehd, myspleen, torrentleech, morethantv, bibliotik, alpharatio, blady, passthepopcorn, brokenstones, pornbay, cgpeers, cinemageddon, broadcasthenet, learnbits, torrentseeds, beyondhd, cinemaz, u2.dmhy, Karagarga, PTerclub, Nyaa.si, Polishtracker, and many more.

Recommended Posts


  • Member ID:  53,629
  • Followers:  0
  • Topic Count:  3,075
  • Topics Per Day:  1.55
  • Content Count:  3,094
  • Content Per Day:  1.56
  • Reputation:   105
  • Achievement Points:  4,020
  • Days Won:  0
  • Joined:  01/24/2021
  • Status:  Offline
  • Last Seen:  

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:

Both matrices have the same number of rows and columns.

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:

Run a nested loop to traverse through each element of both the matrices.

If any of the corresponding elements of the two matrices are not equal, return false.

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 <bits/stdc++.h> 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 < size1; i++) { for (int j = 0; j < size2; 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 < size1; i++) { for (int j = 0; j < size2; j++) { cout << mat[i][j] << " "; } cout << endl; } } // Driver code int main() { // 1st Matrix int mat1[size1][size2] = { {2, 2, 2, 2}, {2, 2, 2, 2}, {2, 2, 2, 2} }; cout << "Matrix 1:" << endl; printMatrix(mat1); // 2nd Matrix int mat2[size1][size2] = { {2, 2, 2, 2}, {2, 2, 2, 2}, {2, 2, 2, 2} }; cout << "Matrix 2:" << endl; printMatrix(mat2); if(isIdentical(mat1, mat2)) { cout << "Yes, the matrices are identical" << endl; } else { cout << "No, the matrices are not identical" << endl; } // 3rd Matrix int mat3[size1][size2] = { {3, 3, 3, 3}, {3, 3, 3, 3}, {3, 3, 3, 3} }; cout << "Matrix 3:" << endl; printMatrix(mat3); // 4th Matrix int mat4[size1][size2] = { {4, 4, 4, 4}, {4, 4, 4, 4}, {4, 4, 4, 4} }; cout << "Matrix 4:" << endl; printMatrix(mat4); if(isIdentical(mat3, mat4)) { cout << "Yes, the matrices are identical" << endl; } else { cout << "No, the matrices are not identical" << endl; } 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 < size1; i++) { for (let j = 0; j < size2; 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 < size1; i++) { for (let j = 0; j < size2; j++) { document.write(mat[i][j] + " "); } document.write("<br>"); } } // Driver code // 1st Matrix var mat1 = [ [2, 2, 2, 2], [2, 2, 2, 2], [2, 2, 2, 2] ]; document.write("Matrix 1:" + "<br>"); printMatrix(mat1); // 2nd Matrix var mat2 = [ [2, 2, 2, 2], [2, 2, 2, 2], [2, 2, 2, 2] ]; document.write("Matrix 2:" + "<br>"); printMatrix(mat2); if (isIdentical(mat1, mat2)) { document.write("Yes, the matrices are identical" + "<br>"); } else{ document.write("No, the matrices are not identical" + "<br>"); } // 3rd Matrix var mat3 = [ [3, 3, 3, 3], [3, 3, 3, 3], [3, 3, 3, 3] ]; document.write("Matrix 3:" + "<br>"); printMatrix(mat3); // 4th Matrix var mat4 = [ [4, 4, 4, 4], [4, 4, 4, 4], [4, 4, 4, 4] ]; document.write("Matrix 4:" + "<br>"); printMatrix(mat4); if (isIdentical(mat3, mat4)) { document.write("Yes, the matrices are identical" + "<br>"); } else{ document.write("No, the matrices are not identical" + "<br>"); }

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 <stdio.h> #include <stdbool.h> // 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 < size1; i++) { for (int j = 0; j < size2; 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 < size1; i++) { for (int j = 0; j < size2; 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.


  • Member ID:  48,211
  • Followers:  60
  • Topic Count:  144
  • Topics Per Day:  0.06
  • Content Count:  30,039
  • Content Per Day:  13.22
  • Reputation:   2,202
  • Achievement Points:  38,180
  • Days Won:  11
  • Joined:  04/09/2020
  • Status:  Offline
  • Last Seen:  

Avoid unnecessary posts such as 'Thank you', 'Welcome', etc. Such posts will be deleted and user will be warned if it happens again. If caught spamming, the following actions are applicable -

  • First time - Warning
  • Second time - 5000 Points will be deducted
  • Third time - Ban for 7 days
  • Fourth time - Permanent Ban

If the post helped you, reward the user by reacting to the post like this -

1.jpg

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Read this before posting -
  • Only post if you have something valuable to contribute.
  • Avoid unnecessary posts such as 'Thank you', 'Welcome', etc. Such posts will be deleted and you will be warned if it happens again.
  • If the post helped you, reward the user by reacting to the post like this -                      1.jpg
Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Customer Reviews

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.