{"id":20185,"date":"2021-09-06T13:42:11","date_gmt":"2021-09-06T08:12:11","guid":{"rendered":"https:\/\/python-programs.com\/?p=20185"},"modified":"2021-11-22T18:36:25","modified_gmt":"2021-11-22T13:06:25","slug":"python-program-to-check-two-matrix-are-equal-or-not","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-check-two-matrix-are-equal-or-not\/","title":{"rendered":"Python Program to Check Two Matrix are Equal or Not"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Check Whether a Matrix is a Scalar or Not<\/a><\/p>\n Given two matrixes and the task is to check if the given two matrixes are equal or not in Python.<\/p>\n What is a matrix:<\/strong><\/p>\n A matrix is a rectangular sequence of numbers divided into columns and rows. A matrix element or entry is a number that appears in a matrix.<\/p>\n Example:<\/strong><\/p>\n <\/p>\n Above is the matrix which contains 5 rows and 4 columns and having elements from 1 to 20.<\/p>\n In this order, the dimensions of a matrix indicate the number of rows and columns.<\/p>\n Here as there are 5 rows and 4 columns it is called a 5*4 matrix.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to check if the given two matrixes are equal or not in Python.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Access the big list of Python Programming Code Examples<\/a> with actual logical code asked in Programming and Coding Interviews for Python and stand out from the crowd.<\/p>\n In the previous article, we have discussed Python Program to Check Whether a Matrix is a Scalar or Not Given two matrixes and the task is to check if the given two matrixes are equal or not in Python. What is a matrix: A matrix is a rectangular sequence of numbers divided into columns and …<\/p>\nGiven first matrix :\r\n5 2 0\r\n0 5 0\r\n0 0 5\r\nGiven second matrix :\r\n5 2 0\r\n0 5 0\r\n0 0 5<\/pre>\n
The given first and second matrixes are equal.<\/pre>\n
Given first matrix :\r\n3 5\r\n4 6\r\nGiven second matrix :\r\n1 2\r\n4 6<\/pre>\n
The given first and second matrixes are not equal.<\/pre>\n
Program to Check Two Matrix are Equal or Not in Python<\/h2>\n
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
# Give the first matrix as static input and store it in a variable.\r\nfst_mtrx = [[5, 2, 0], [0, 5, 0], [0, 0, 5]]\r\n# Calculate the number of rows of the given first matrix by\r\n# calculating the length of the nested list using the len() function\r\n# and store it in a variable mtrxrows.\r\nmtrxrows = len(fst_mtrx)\r\n# Calculate the number of columns of the given matrix by\r\n# calculating the length of the first list in the nested list\r\n# using the len() function and store it in a variable mtrxcols.\r\nmtrxcols = len(fst_mtrx[0])\r\n# Give the second matrix as static input and store it in a variable.\r\nscnd_mtrx = [[5, 2, 0], [0, 5, 0], [0, 0, 5]]\r\n# Take a variable say tempo and initialize its value to zero.\r\ntempo = 0\r\n# Loop till the given number of rows using the For loop.\r\nfor n in range(mtrxrows):\r\n # Inside the For loop, Iterate till the given number of columns using another\r\n # Nested For loop(Inner For loop).\r\n for m in range(mtrxcols):\r\n # Check if the condition firstmatrix[n][m] is not equal to the secondmatrix[n][m] using\r\n # the if conditional statement where n is the iterator value of the parent For loop\r\n # and m\u00a0is the iterator value of the inner For loop.\r\n if fst_mtrx[n][m] != scnd_mtrx[n][m]:\r\n # If the statement is true, then make the value of tempo as 1.\r\n tempo = 1\r\n # Break the statement and exit the loop.\r\n break\r\n# Check if the value of tempo is equal to 1 using the if conditional statement. break\r\nif tempo == 1:\r\n # If the statement is true, then print \"The given first and second matrixes are not equal.\"\r\n print(\"The given first and second matrixes are not equal.\")\r\nelse:\r\n # Else print \"The given first and second matrixes are equal.\"\r\n print(\"The given first and second matrixes are equal.\")\r\n<\/pre>\n
The given first and second matrixes are equal.<\/pre>\n
Method #2: Using For loop (User Input)<\/h3>\n
\n
# Give the number of rows of the matrix as user input using the int(input()) function\r\n# and store it in a variable.\r\nmtrxrows = int(input('Enter some random number of rows of the matrix = '))\r\n# Give the number of columns of the matrix as user input using the int(input()) function\r\n# and store it in another variable.\r\nmtrxcols = int(input('Enter some random number of columns of the matrix = '))\r\n# Take a list and initialize it with an empty value using [] or list() to say gvnmatrix.\r\nfst_mtrx = []\r\nprint(\"Enter elements of the first matrix :\")\r\n# Loop till the given number of rows using the For loop\r\nfor n in range(mtrxrows):\r\n # Inside the For loop, Give all the row elements of the given Matrix as a list using\r\n # the list(),map(),int(),split() functions and store it in a variable.\r\n l = list(map(int, input(\r\n 'Enter {'+str(mtrxcols)+'} elements of row {'+str(n+1)+'} separated by spaces = ').split()))\r\n # Add the above row elements list to gvnmatrix using the append() function.\r\n\r\n fst_mtrx.append(l)\r\nscnd_mtrx = []\r\nprint(\"Enter elements of the second matrix :\")\r\n# Loop till the given number of rows using the For loop\r\nfor n in range(mtrxrows):\r\n # Inside the For loop, Give all the row elements of the given Matrix as a list using\r\n # the list(),map(),int(),split() functions and store it in a variable.\r\n l = list(map(int, input(\r\n 'Enter {'+str(mtrxcols)+'} elements of row {'+str(n+1)+'} separated by spaces = ').split()))\r\n # Add the above row elements list to gvnmatrix using the append() function.\r\n\r\n scnd_mtrx.append(l)\r\n \r\n# Take a variable say tempo and initialize its value to zero.\r\ntempo = 0\r\n# Loop till the given number of rows using the For loop.\r\nfor n in range(mtrxrows):\r\n # Inside the For loop, Iterate till the given number of columns using another\r\n # Nested For loop(Inner For loop).\r\n for m in range(mtrxcols):\r\n # Check if the condition firstmatrix[n][m] is not equal to the secondmatrix[n][m] using\r\n # the if conditional statement where n is the iterator value of the parent For loop\r\n # and m\u00a0is the iterator value of the inner For loop.\r\n if fst_mtrx[n][m] != scnd_mtrx[n][m]:\r\n # If the statement is true, then make the value of tempo as 1.\r\n tempo = 1\r\n # Break the statement and exit the loop.\r\n break\r\n# Check if the value of tempo is equal to 1 using the if conditional statement. break\r\nif tempo == 1:\r\n # If the statement is true, then print \"The given first and second matrixes are not equal.\"\r\n print(\"The given first and second matrixes are not equal.\")\r\nelse:\r\n # Else print \"The given first and second matrixes are equal.\"\r\n print(\"The given first and second matrixes are equal.\")\r\n\r\n \r\n\r\n<\/pre>\n
Enter some random number of rows of the matrix = 2\r\nEnter some random number of columns of the matrix = 2\r\nEnter elements of the first matrix :\r\nEnter {2} elements of row {1} separated by spaces = 3 5\r\nEnter {2} elements of row {2} separated by spaces = 4 6\r\nEnter elements of the second matrix :\r\nEnter {2} elements of row {1} separated by spaces = 1 2\r\nEnter {2} elements of row {2} separated by spaces = 4 6\r\nThe given first and second matrixes are not equal.<\/pre>\n
\n