Sum of each row and each column in python

Question:

Hi I have more than 20 txt file that include a matrix (9*7) 9 rows and 7 columns:
I want to find sum of each 7 rows and 9 columns for each matrix
My code that I have used is for one matrix how can I use for multi matrix is there any way with python?

   import numpy as np

  # Get the size m and n 

m , n = 7, 9      

    # Function to calculate sum of each row 

  def row_sum(arr) :


sum = 0


print("nFinding Sum of each 
 row:n")


# finding the row sum 

for i in range(m) :

    for j in range(n) :


        # Add the element 

        sum += arr[i][j]


    # Print the row sum 

    print("Sum of the 
   row",i,"=",sum)


    # Reset the sum 

    sum = 0


    # Function to calculate sum of 
  each column 

def column_sum(arr) :


sum = 0


print("nFinding Sum of each 
  column:n")


# finding the column sum 

for i in range(m) :

    for j in range(n) :


        # Add the element 

        sum += arr[j][i]


    # Print the column sum

    print("Sum of the 
  column",i,"=",sum)


    # Reset the sum 

    sum = 0


     

    # Driver code     

    if __name__ == "__main__" :


arr = np.zeros((4, 4))


# Get the matrix elements 

x = 1

 

for i in range(m) :

    for j in range(n) :

        arr[i][j] = x


        x += 1

             

# Get each row sum 

row_sum(arr)


# Get each column sum 

column_sum(arr)

And I want the output of each the sum be a vector for each matrix sth like this :

[ 1,2,3,4,5,6,7,8,9,10,…,16]

Asked By: FTh

||

Answers:

To calculate the row and column sums for multiple matrices, you can create a function that takes a list of matrices and calculates the row and column sums for each matrix in the list. Here is an example:

import numpy as np

# Get the size m and n
m, n = 7, 9

# Function to calculate sum of each row
def row_sum(arr):
    sums = []
    for i in range(m):
        row_sum = 0
        for j in range(n):
            row_sum += arr[i][j]
        sums.append(row_sum)
    return sums

# Function to calculate sum of each column
def column_sum(arr):
    sums = []
    for i in range(m):
        column_sum = 0
        for j in range(n):
            column_sum += arr[j][i]
        sums.append(column_sum)
    return sums

# Driver code
if __name__ == "__main__":
    arr = np.zeros((4, 4))

    # Get the matrix elements
    x = 1
    for i in range(m):
        for j in range(n):
            arr[i][j] = x
            x += 1

    # Get each row sum
    row_sums = row_sum(arr)
    print("Row sums:", row_sums)

    # Get each column sum
    column_sums = column_sum(arr)
    print("Column sums:", column_sums)

To find the row and column sums for multiple matrices, you can loop through the matrices and calculate the row and column sums for each one, storing the results in a list. For example:

# Get the size m and n
m, n = 7, 9

# Function to calculate sum of each row
def row_sum(arr):
    sums = []
    for i in range(m):
        row_sum = 0
        for j in range(n):
            row_sum += arr[i][j]
        sums.append(row_sum)
    return sums

# Function to calculate sum of each column
def column_sum(arr):
    sums = []
    for i in range(m):
        column_sum = 0
        for j in range(n):
            column_sum += arr[j][i]
        sums.append(column_sum)
    return sums



Answered By: kenjoe41
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.