{"id":22039,"date":"2021-09-22T17:11:46","date_gmt":"2021-09-22T11:41:46","guid":{"rendered":"https:\/\/python-programs.com\/?p=22039"},"modified":"2021-11-22T18:36:15","modified_gmt":"2021-11-22T13:06:15","slug":"python-program-to-count-frequency-of-k-in-a-matrix-of-size-n-where-matrix-i-j-ij","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-count-frequency-of-k-in-a-matrix-of-size-n-where-matrix-i-j-ij\/","title":{"rendered":"Python Program to Count Frequency of k in a Matrix of Size n Where Matrix (i, j) = i+j"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Calculate Sum of all Maximum Occurring Elements in Matrix<\/a><\/p>\n Given a number n (which is the size of the matrix)and another number k, the task is to find the frequency of k of the matrix with size n and matrix (i,j)=i+j.<\/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 find the frequency of k of the matrix with size n and matrix (i,j)=i+j 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 Dive into numerous Python Programming Language Examples<\/a> for practice and get the best out of the tutorial and learn python one step at a time.<\/p>\n In the previous article, we have discussed Python Program to Calculate Sum of all Maximum Occurring Elements in Matrix Given a number n (which is the size of the matrix)and another number k, the task is to find the frequency of k of the matrix with size n and matrix (i,j)=i+j. What is a matrix: …<\/p>\nGiven size of matrix = 3\r\nGiven k value = 6<\/pre>\n
The given k value{ 6 } frequency in the matix = 1<\/pre>\n
Given size of matrix = 4\r\nGiven k value = 3<\/pre>\n
The given k value{ 3 } frequency in the matix = 2<\/pre>\n
Program to Count Frequency of k in a Matrix of Size n Where Matrix(i, j) = i+j in Python<\/h2>\n
\n
Method #1: Using Mathematical Formula (Static Input)<\/h3>\n
\n
# Import math module using the import keyword.\r\nimport math\r\n\r\n# Create a function to say findfreqencyof_k() which takes the given number(size of matrix)\r\n# and the given k value as the arguments and returns the frequency of k of the matrix\r\n# with size n and matrix (i,j)=i+j.\r\n\r\n\r\ndef findfreqencyof_k(gvn_numb, gvn_k_val):\r\n # Inside the function, check if the given number+1 is greater than or equal to the given\r\n # k value using the if conditional statement.\r\n if (gvn_numb + 1 >= gvn_k_val):\r\n # If it is true, then return given k value -1.\r\n return (gvn_k_val - 1)\r\n else:\r\n # Else return (2 * gvn_numb + 1 - gvn_k_val).\r\n return (2 * gvn_numb + 1 - gvn_k_val)\r\n\r\n\r\n# Give the number(size of matrix) as static input and store it in a variable.\r\ngvn_numb = 3\r\n# Give another number k as static input and store it in another variable.\r\ngvn_k_val = 6\r\n# Pass the given number(size of matrix) and the given k value as the arguments to the\r\n# findfreqencyof_k() function and store it in another variable.\r\nrslt = findfreqencyof_k(gvn_numb, gvn_k_val)\r\n# Check if the above result is less than 0 using the if conditional statement.\r\nif (rslt < 0):\r\n # If it is true, then print \"The given element k does not exist in the matrix\".\r\n print(\"The given element{\", gvn_k_val, \"} does not exist in the matrix\")\r\nelse:\r\n # Else print the frequency of the given k value.\r\n print(\" The given k value{\", gvn_k_val,\r\n \"} frequency in the matix = \", rslt)\r\n<\/pre>\n
The given k value{ 6 } frequency in the matix = 1<\/pre>\n
Method #2: Using Mathematical Formula (User Input)<\/h3>\n
\n
# Import math module using the import keyword.\r\nimport math\r\n\r\n# Create a function to say findfreqencyof_k() which takes the given number(size of matrix)\r\n# and the given k value as the arguments and returns the frequency of k of the matrix\r\n# with size n and matrix (i,j)=i+j.\r\n\r\n\r\ndef findfreqencyof_k(gvn_numb, gvn_k_val):\r\n # Inside the function, check if the given number+1 is greater than or equal to the given\r\n # k value using the if conditional statement.\r\n if (gvn_numb + 1 >= gvn_k_val):\r\n # If it is true, then return given k value -1.\r\n return (gvn_k_val - 1)\r\n else:\r\n # Else return (2 * gvn_numb + 1 - gvn_k_val).\r\n return (2 * gvn_numb + 1 - gvn_k_val)\r\n\r\n\r\n# Give the number(size of matrix) as user input using the int(input()) function \r\n# and store it in a variable.\r\ngvn_numb = int(input(\"Enter some random number = \"))\r\n# Give another number k as user input using the int(input()) function and\r\n# store it in another variable.\r\ngvn_k_val = int(input(\"Enter some random number = \"))\r\n# Pass the given number(size of matrix) and the given k value as the arguments to the\r\n# findfreqencyof_k() function and store it in another variable.\r\nrslt = findfreqencyof_k(gvn_numb, gvn_k_val)\r\n# Check if the above result is less than 0 using the if conditional statement.\r\nif (rslt < 0):\r\n # If it is true, then print \"The given element k does not exist in the matrix\".\r\n print(\"The given element{\", gvn_k_val, \"} does not exist in the matrix\")\r\nelse:\r\n # Else print the frequency of the given k value.\r\n print(\" The given k value{\", gvn_k_val,\r\n \"} frequency in the matix = \", rslt)\r\n<\/pre>\n
Enter some random number = 4\r\nEnter some random number = 3\r\nThe given k value{ 3 } frequency in the matix = 2<\/pre>\n
\n