{"id":20141,"date":"2021-09-06T13:37:48","date_gmt":"2021-09-06T08:07:48","guid":{"rendered":"https:\/\/python-programs.com\/?p=20141"},"modified":"2021-11-22T18:36:27","modified_gmt":"2021-11-22T13:06:27","slug":"python-program-to-check-whether-a-matrix-is-diagonal-or-not","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-check-whether-a-matrix-is-diagonal-or-not\/","title":{"rendered":"Python Program to Check Whether a Matrix is Diagonal or Not"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Find Square of a Matrix<\/a><\/p>\n Given a matrix and the task is to check if the given matrix is diagonal 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 Diagonal Matrix:<\/strong><\/p>\n The entries outside the main diagonal of a diagonal matrix are all 0; the word usually refers to square matrices.<\/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 matrix is diagonal 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 Grab the opportunity and utilize the Python Program Code Examples<\/a> over here to prepare basic and advanced topics too with ease and clear all your doubts.<\/p>\n In the previous article, we have discussed Python Program to Find Square of a Matrix Given a matrix and the task is to check if the given matrix is diagonal or not in Python. What is a matrix: A matrix is a rectangular sequence of numbers divided into columns and rows. A matrix element or …<\/p>\nGiven Matrix : \r\n3 0 0\r\n0 1 0\r\n0 0 2<\/pre>\n
The Matrix given is a diagonal Matrix.<\/pre>\n
Given Matrix : \r\n1 2 3 \r\n6 5 4 \r\n7 8 9<\/pre>\n
The Matrix given is not a diagonal Matrix.<\/pre>\n
Program to Check Whether a Matrix is Diagonal or Not in Python<\/h2>\n
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
# Give the matrix as static input and store it in a variable.\r\nmtrx = [[3, 0, 0], [0, 1, 0], [0, 0, 2]]\r\n# Calculate the number of rows of the given 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(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(mtrx[0])\r\n# Take a variable and initialize its value to zero.\r\ntempo = 0\r\n# To print all the elements of the given 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, 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 n is not equal to m and gvnmatrix[n][m] not equal to 0 using the\r\n # if conditional statement where n is the iterator value of the parent For loop and m\r\n # is the iterator value of the inner For loop.\r\n if n != m and mtrx[n][m] != 0:\r\n # If the statement is true, then make the value of\u00a0tempo as 1.\r\n tempo = 1\r\n # Break the statement and come out of the loop.\r\n break\r\n# Check if the value of tempo is equal to 1 using the if conditional statement.\r\nif tempo == 1:\r\n # If the statement is true, then print \"The Matrix given is not a diagonal Matrix.\"\r\n print(\"The Matrix given is not a diagonal Matrix.\")\r\nelse:\r\n # Else print \"The Matrix given is a diagonal Matrix.\"\r\n print(\"The Matrix given is a diagonal Matrix.\")\r\n<\/pre>\n
The Matrix given is a diagonal Matrix.<\/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\nmtrx = []\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 mtrx.append(l)\r\n \r\n# Take a variable say tempo and initialize its value to zero.\r\ntempo = 0\r\n# To print all the elements of the given 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, 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 n is not equal to m and gvnmatrix[n][m] not equal to 0 using the\r\n # if conditional statement where n is the iterator value of the parent For loop and m\r\n # is the iterator value of the inner For loop.\r\n if n != m and mtrx[n][m] != 0:\r\n # If the statement is true, then make the value of\u00a0tempo as 1.\r\n tempo = 1\r\n # Break the statement and come out of the loop.\r\n break\r\n# Check if the value of tempo is equal to 1 using the if conditional statement.\r\nif tempo == 1:\r\n # If the statement is true, then print \"The Matrix given is not a diagonal Matrix.\"\r\n print(\"The Matrix given is not a diagonal Matrix.\")\r\nelse:\r\n # Else print \"The Matrix given is a diagonal Matrix.\"\r\n print(\"The Matrix given is a diagonal Matrix.\")\r\n<\/pre>\n
Enter some random number of rows of the matrix = 3\r\nEnter some random number of columns of the matrix = 3\r\nEnter {3} elements of row {1} separated by spaces = 1 2 3\r\nEnter {3} elements of row {2} separated by spaces = 6 5 4\r\nEnter {3} elements of row {3} separated by spaces = 7 8 9\r\nThe Matrix given is not a diagonal Matrix.<\/pre>\n
\n