{"id":20820,"date":"2021-09-22T17:32:38","date_gmt":"2021-09-22T12:02:38","guid":{"rendered":"https:\/\/python-programs.com\/?p=20820"},"modified":"2021-11-22T18:36:13","modified_gmt":"2021-11-22T13:06:13","slug":"python-program-for-markov-matrix","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-for-markov-matrix\/","title":{"rendered":"Python Program for Markov Matrix"},"content":{"rendered":"
In the previous article, we have discussed Python Program for Sum of Middle Row and Column in Matrix<\/a><\/p>\n Markov Matrix:<\/strong><\/p>\n The matrix where the sum of each row equals one.<\/p>\n Given a matrix, the task is to check if the given matrix is a Markov matrix or not.<\/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 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 a Markov matrix 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 for Sum of Middle Row and Column in Matrix Markov Matrix: The matrix where the sum of each row equals one. Given a matrix, the task is to check if the given matrix is a Markov matrix or not. What is a matrix: A matrix is …<\/p>\nGiven Matrix : \r\n0.5 0.5 0\r\n0 0 1\r\n1 0 0\r\n\r\n<\/pre>\n
Yes, the given matrix is a Markov Matrix<\/pre>\n
Given Matrix : \r\n1 2 0\r\n0 0.5 0.5\r\n1 0 0<\/pre>\n
No, the given matrix is not a Markov Matrix<\/pre>\n
Program for Markov Matrix in Python<\/h2>\n
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
# Create a function to say chek_Markovmatrix which takes the given matrix as an argument\r\n# and returns True or false.\r\n\r\n\r\ndef chek_Markovmatrix(mtrx):\r\n\r\n # Loop till the given number of rows using the For loop.\r\n for p in range(mtrxrows):\r\n # Take a variable to say row_summ and initialize its value to 0.\r\n\r\n row_summ = 0\r\n # Inside the For loop, Iterate till the given number of rows using another Nested\r\n # For loop (Inner For loop).\r\n for q in range(mtrxrows):\r\n # Add mtrx[p][q] to the above row_summ (where p is the iterator value of the\r\n # parent for loop and q is the iterator value of the inner forloop).\r\n # Store it in the same variable\u00a0row_summ\r\n row_summ = row_summ + mtrx[p][q]\r\n # Check if the value of row_summ is not equal to 1 using the if conditional\r\n # statement.\r\n if (row_summ != 1):\r\n # If it is true then return false.\r\n return False\r\n # Return True.\r\n return True\r\n\r\n\r\n# Give the matrix as static input and store it in a variable.\r\nmtrx = [[0.5, 0.5, 0], [0, 0, 1], [1, 0, 0]]\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# Pass the given matrix as an argument to the chek_Markovmatrix function and check if\r\n# the function returns true or false using the if conditional statement.\r\nif (chek_Markovmatrix(mtrx)):\r\n # If it is true then print\u00a0 \"Yes, the given matrix is a Markov Matrix\".\r\n print(\"Yes, the given matrix is a Markov Matrix\")\r\nelse:\r\n # Else print \"No, the given matrix is not a Markov Matrix\".\r\n print(\"No, the given matrix is not a Markov Matrix\")\r\n<\/pre>\n
Yes, the given matrix is a Markov Matrix<\/pre>\n
Method #2: Using For loop (User Input)<\/h3>\n
\n
# Create a function to say chek_Markovmatrix which takes the given matrix as an argument\r\n# and returns True or false.\r\n\r\n\r\ndef chek_Markovmatrix(mtrx):\r\n\r\n # Loop till the given number of rows using the For loop.\r\n for p in range(mtrxrows):\r\n # Take a variable to say row_summ and initialize its value to 0.\r\n\r\n row_summ = 0\r\n # Inside the For loop, Iterate till the given number of rows using another Nested\r\n # For loop (Inner For loop).\r\n for q in range(mtrxrows):\r\n # Add mtrx[p][q] to the above row_summ (where p is the iterator value of the\r\n # parent for loop and q is the iterator value of the inner forloop).\r\n # Store it in the same variable\u00a0row_summ\r\n row_summ = row_summ + mtrx[p][q]\r\n # Check if the value of row_summ is not equal to 1 using the if conditional\r\n # statement.\r\n if (row_summ != 1):\r\n # If it is true then return false.\r\n return False\r\n # Return True.\r\n return True\r\n\r\n\r\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(),float(),split() functions and store it in a variable.\r\n l = list(map(float, 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# Pass the given matrix as an argument to the chek_Markovmatrix function and check if\r\n# the function returns true or false using the if conditional statement.\r\nif (chek_Markovmatrix(mtrx)):\r\n # If it is true then print\u00a0 \"Yes, the given matrix is a Markov Matrix\".\r\n print(\"Yes, the given matrix is a Markov Matrix\")\r\nelse:\r\n # Else print \"No, the given matrix is not a Markov Matrix\".\r\n print(\"No, the given matrix is not a Markov 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 0\r\nEnter {3} elements of row {2} separated by spaces = 0 0.5 0.5\r\nEnter {3} elements of row {3} separated by spaces = 1 0 0\r\nNo, the given matrix is not a Markov Matrix<\/pre>\n
\n