Python Program for Sum of Middle Row and Column in Matrix

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 divided into columns and rows. A matrix element or entry is a number that appears in a matrix.

Example:

Above is the matrix which contains 5 rows and 4 columns and having elements from 1 to 20.

In this order, the dimensions of a matrix indicate the number of rows and columns.

Here as there are 5 rows and 4 columns it is called a 5*4 matrix.

Examples:

Example1:

Input:

Given Matrix :
4 2 0
4 2 3
5 6 9

Output:

The sum of middle row elements of the above given matrix =  9
The sum of middle column elements of the above given matrix =  10

Example2:

Input:

Given Matrix :
0 1 3
4 5 6
8 2 1

Output:

The sum of middle row elements of the above given matrix = 15
The sum of middle column elements of the above given matrix = 8

Program for Sum of Middle Row and Column in Matrix in Python

Below are the ways to get the sum of middle row elements and the middle column elements of a given matrix in python:

Method #1: Using For Loop (Static Input)

Approach:

  • Give the matrix as static input and store it in a variable.
  • Calculate the number of rows of the given matrix by calculating the length of the nested list using the len() function and store it in a variable mtrxrows.
  • Calculate the number of columns of the given matrix by calculating the length of the first list in the nested list using the len() function and store it in a variable mtrxcolums.
  • Take a variable to say middlrow_sum and initialize its value to 0.
  • Take a variable to say middlcol_sum and initialize its value to 0.
  • Loop till the given number of rows using the For loop.
  • Inside the for loop, add mtrx[mtrxrows // 2][itr] to the above-initialized middlrow_sum where itr is the iterator value of the for loop.
  • Print the sum of the middle row elements of the given matrix.
  • Loop till the given number of rows using the For loop.
  • Inside the for loop, add mtrx[itr][mtrxrows // 2]  to the above-initialized middlcol_sum where itr is the iterator value of the for loop.
  • Print the sum of the middle column elements of the given matrix.
  • The Exit of the Program.

Below is the implementation:

# Give the matrix as static input and store it in a variable.
mtrx = [[4, 2, 0], [4, 2, 3], [5, 6, 9]]
# Calculate the number of rows of the given matrix by
# calculating the length of the nested list using the len() function
# and store it in a variable mtrxrows.
mtrxrows = len(mtrx)
# Calculate the number of columns of the given matrix by
# calculating the length of the first list in the nested list
# using the len() function and store it in a variable mtrxcols.
mtrxcols = len(mtrx[0])
# Take a variable to say middlrow_sum and initialize its value to 0.
middlrow_sum = 0
# Take a variable to say middlcol_sum and initialize its value to 0.
middlcol_sum = 0
# Loop till the given number of rows using the For loop.
for itr in range(mtrxrows):
  # Inside the for loop, add mtrx[mtrxrows // 2][itr] to the above-initialized middlrow_sum
  # where itr is the iterator value of the for loop.

    middlrow_sum += mtrx[mtrxrows // 2][itr]
# Print the sum of the middle row elements of the given matrix.
print("The sum of middle row elements of the above given matrix = ",
      middlrow_sum)
# Loop till the given number of rows using the For loop.
for itr in range(mtrxrows):
  # Inside the for loop, add mtrx[itr][mtrxrows // 2]  to the above-initialized middlcol_sum
  # where itr is the iterator value of the for loop.

    middlcol_sum += mtrx[itr][mtrxrows // 2]
# Print the sum of the middle column elements of the given matrix.
print("The sum of middle column elements of the above given matrix = ",
      middlcol_sum)

Output:

The sum of middle row elements of the above given matrix =  9
The sum of middle column elements of the above given matrix =  10

Method #2: Using For loop (User Input)

Approach:

  • Give the number of rows of the matrix as user input using the int(input()) function and store it in a variable.
  • Give the number of columns of the matrix as user input using the int(input()) function and store it in another variable.
  • Take a list and initialize it with an empty value using [] or list() to say gvnmatrix.
  • Loop till the given number of rows using the For loop
  • Inside the For loop, Give all the row elements of the given Matrix as a list using the list(),map(),int(),split() functions and store it in a variable.
  • Add the above row elements list to gvnmatrix using the append() function.
  • Take a variable to say middlrow_sum and initialize its value to 0.
  • Take a variable to say middlcol_sum and initialize its value to 0.
  • Loop till the given number of rows using the For loop.
  • Inside the for loop, add mtrx[mtrxrows // 2][itr] to the above-initialized middlrow_sum where itr is the iterator value of the for loop.
  • Print the sum of the middle row elements of the given matrix.
  • Loop till the given number of rows using the For loop.
  • Inside the for loop, add mtrx[itr][mtrxrows // 2]  to the above-initialized middlcol_sum where itr is the iterator value of the for loop.
  • Print the sum of the middle column elements of the given matrix.
  • The Exit of the Program.

Below is the implementation:

# Give the number of rows of the matrix as user input using the int(input()) function
# and store it in a variable.
mtrxrows = int(input('Enter some random number of rows of the matrix = '))
# Give the number of columns of the matrix as user input using the int(input()) function
# and store it in another variable.
mtrxcols = int(input('Enter some random number of columns of the matrix = '))
# Take a list and initialize it with an empty value using [] or list() to say gvnmatrix.
mtrx = []
# Loop till the given number of rows using the For loop
for n in range(mtrxrows):
    # Inside the For loop, Give all the row elements of the given Matrix as a list using
    # the list(),map(),int(),split() functions and store it in a variable.
    l = list(map(int, input(
        'Enter {'+str(mtrxcols)+'} elements of row {'+str(n+1)+'} separated by spaces = ').split()))
    # Add the above row elements list to gvnmatrix using the append() function.

    mtrx.append(l)
    

# Take a variable to say middlrow_sum and initialize its value to 0.
middlrow_sum = 0
# Take a variable to say middlcol_sum and initialize its value to 0.
middlcol_sum = 0
# Loop till the given number of rows using the For loop.
for itr in range(mtrxrows):
  # Inside the for loop, add mtrx[mtrxrows // 2][itr] to the above-initialized middlrow_sum
  # where itr is the iterator value of the for loop.

    middlrow_sum += mtrx[mtrxrows // 2][itr]
# Print the sum of the middle row elements of the given matrix.
print("The sum of middle row elements of the above given matrix = ",
      middlrow_sum)
# Loop till the given number of rows using the For loop.
for itr in range(mtrxrows):
  # Inside the for loop, add mtrx[itr][mtrxrows // 2]  to the above-initialized middlcol_sum
  # where itr is the iterator value of the for loop.

    middlcol_sum += mtrx[itr][mtrxrows // 2]
# Print the sum of the middle column elements of the given matrix.
print("The sum of middle column elements of the above given matrix = ",
      middlcol_sum)

Output:

Enter some random number of rows of the matrix = 3
Enter some random number of columns of the matrix = 3
Enter {3} elements of row {1} separated by spaces = 0 1 3
Enter {3} elements of row {2} separated by spaces = 4 5 6
Enter {3} elements of row {3} separated by spaces = 8 2 1
The sum of middle row elements of the above given matrix = 15
The sum of middle column elements of the above given matrix = 8

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. Using them you can develop code on your own and master coding skills.