{"id":20809,"date":"2021-09-22T17:32:33","date_gmt":"2021-09-22T12:02:33","guid":{"rendered":"https:\/\/python-programs.com\/?p=20809"},"modified":"2021-11-22T18:36:13","modified_gmt":"2021-11-22T13:06:13","slug":"python-program-for-sum-of-middle-row-and-column-in-matrix","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-for-sum-of-middle-row-and-column-in-matrix\/","title":{"rendered":"Python Program for Sum of Middle Row and Column in Matrix"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Interchange Elements of First and Last Rows in Matrix<\/a><\/p>\n Given a matrix and the task is to get the sum of middle row elements and the middle column elements of a given matrix.<\/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 get the sum of middle row elements and the middle column elements of a given matrix 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 The best way to learn Python for Beginners is to practice as much as they can taking help of the Sample Python Programs For Beginners<\/a>. Using them you can develop code on your own and master coding skills.<\/p>\n In the previous article, we have discussed Python Program to Interchange Elements of First and Last Rows in Matrix Given a matrix and the task is to get the sum of middle row elements and the middle column elements of a given matrix. What is a matrix: A matrix is a rectangular sequence of numbers …<\/p>\nGiven Matrix :\r\n4 2 0\r\n4 2 3\r\n5 6 9\r\n\r\n<\/pre>\n
The sum of middle row elements of the above given matrix = 9\r\nThe sum of middle column elements of the above given matrix = 10<\/pre>\n
Given Matrix :\r\n0 1 3\r\n4 5 6\r\n8 2 1<\/pre>\n
The sum of middle row elements of the above given matrix = 15\r\nThe sum of middle column elements of the above given matrix = 8<\/pre>\n
Program for Sum of Middle Row and Column in Matrix 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 = [[4, 2, 0], [4, 2, 3], [5, 6, 9]]\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 to say middlrow_sum and initialize its value to 0.\r\nmiddlrow_sum = 0\r\n# Take a variable to say middlcol_sum and initialize its value to 0.\r\nmiddlcol_sum = 0\r\n# Loop till the given number of rows using the For loop.\r\nfor itr in range(mtrxrows):\r\n # Inside the for loop, add mtrx[mtrxrows \/\/ 2][itr] to the above-initialized middlrow_sum\r\n # where itr is the iterator value of the for loop.\r\n\r\n middlrow_sum += mtrx[mtrxrows \/\/ 2][itr]\r\n# Print the sum of the middle row elements of the given matrix.\r\nprint(\"The sum of middle row elements of the above given matrix = \",\r\n middlrow_sum)\r\n# Loop till the given number of rows using the For loop.\r\nfor itr in range(mtrxrows):\r\n # Inside the for loop, add mtrx[itr][mtrxrows \/\/ 2]\u00a0 to the above-initialized middlcol_sum\r\n # where itr is the iterator value of the for loop.\r\n\r\n middlcol_sum += mtrx[itr][mtrxrows \/\/ 2]\r\n# Print the sum of the middle column elements of the given matrix.\r\nprint(\"The sum of middle column elements of the above given matrix = \",\r\n middlcol_sum)\r\n<\/pre>\n
The sum of middle row elements of the above given matrix = 9\r\nThe sum of middle column elements of the above given matrix = 10<\/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\r\n# Take a variable to say middlrow_sum and initialize its value to 0.\r\nmiddlrow_sum = 0\r\n# Take a variable to say middlcol_sum and initialize its value to 0.\r\nmiddlcol_sum = 0\r\n# Loop till the given number of rows using the For loop.\r\nfor itr in range(mtrxrows):\r\n # Inside the for loop, add mtrx[mtrxrows \/\/ 2][itr] to the above-initialized middlrow_sum\r\n # where itr is the iterator value of the for loop.\r\n\r\n middlrow_sum += mtrx[mtrxrows \/\/ 2][itr]\r\n# Print the sum of the middle row elements of the given matrix.\r\nprint(\"The sum of middle row elements of the above given matrix = \",\r\n middlrow_sum)\r\n# Loop till the given number of rows using the For loop.\r\nfor itr in range(mtrxrows):\r\n # Inside the for loop, add mtrx[itr][mtrxrows \/\/ 2]\u00a0 to the above-initialized middlcol_sum\r\n # where itr is the iterator value of the for loop.\r\n\r\n middlcol_sum += mtrx[itr][mtrxrows \/\/ 2]\r\n# Print the sum of the middle column elements of the given matrix.\r\nprint(\"The sum of middle column elements of the above given matrix = \",\r\n middlcol_sum)<\/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 = 0 1 3\r\nEnter {3} elements of row {2} separated by spaces = 4 5 6\r\nEnter {3} elements of row {3} separated by spaces = 8 2 1\r\nThe sum of middle row elements of the above given matrix = 15\r\nThe sum of middle column elements of the above given matrix = 8<\/pre>\n
\n