Matrix Utility Functions in Python
Create 2D Array
import numpy as np
def create_2d_array(rows, cols, initial_value=0):
matrix = [Link]((rows, cols), dtype=int)
if initial_value != 0:
for i in range(rows):
for j in range(cols):
matrix[i][j] = initial_value
return [Link]()
# Example
matrix = create_2d_array(3, 3, 5)
print("Created Matrix:")
for row in matrix:
print(row)
Iterate Over Rows
def iterate_rows(matrix):
for row in matrix:
print(row)
# Example
matrix = [[1, 2], [3, 4]]
print("Iterating over rows:")
iterate_rows(matrix)
Iterate Over Columns
def iterate_columns(matrix):
rows = len(matrix)
cols = len(matrix[0])
for j in range(cols):
for i in range(rows):
print(matrix[i][j], end=" ")
print()
# Example
matrix = [[1, 2], [3, 4]]
print("Iterating over columns:")
iterate_columns(matrix)
Sum All Values
def sum_all_values(matrix):
total = 0
for row in matrix:
for value in row:
total += value
return total
# Example
matrix = [[1, 2], [3, 4]]
print("Sum of all values:", sum_all_values(matrix))
Sum of Each Row
def sum_rows(matrix):
row_sums = []
for row in matrix:
row_sums.append(sum(row))
return row_sums
# Example
matrix = [[1, 2], [3, 4]]
print("Row-wise sums:", sum_rows(matrix))
without append
def sum_rows(matrix):
row_sums = [0] * len(matrix) # শুরুতে সব 0 দিয়ে লিস্ট বানানো
for i in range(len(matrix)):
row_sums[i] = sum(matrix[i])
return row_sums
Sum of Each Column
def sum_columns(matrix):
rows = len(matrix)
cols = len(matrix[0])
col_sums = [0] * cols
for j in range(cols):
for i in range(rows):
col_sums[j] += matrix[i][j]
return col_sums
# Example
matrix = [[1, 2], [3, 4]]
print("Column-wise sums:", sum_columns(matrix))
Swap Columns
def swap_columns(matrix):
rows = len(matrix)
cols = len(matrix[0])
for i in range(rows):
for j in range(cols // 2):
temp = matrix[i][j]
matrix[i][j] = matrix[i][cols - 1 - j]
matrix[i][cols - 1 - j] = temp
return matrix
# Example
matrix = [
[1, 2, 3],
[4, 5, 6]
]
print("Before swap:")
for row in matrix:
print(row)
swapped = swap_columns(matrix)
print("After column swap:")
for row in swapped:
print(row)
Sum of Primary Diagonal
def sum_primary_diagonal(matrix):
total = 0
for i in range(len(matrix)):
total += matrix[i][i]
return total
# Example
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
print("Primary diagonal sum:", sum_primary_diagonal(matrix))
Sum of Secondary Diagonal
def sum_secondary_diagonal(matrix):
total = 0
n = len(matrix)
for i in range(n):
total += matrix[i][n - 1 - i]
return total
# Example
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
print("Secondary diagonal sum:", sum_secondary_diagonal(matrix))
Add Two Matrices
import numpy as np
def add_matrices(matrix1, matrix2):
rows = len(matrix1)
cols = len(matrix1[0])
result = [Link]((rows, cols), dtype=int)
for i in range(rows):
for j in range(cols):
result[i][j] = matrix1[i][j] + matrix2[i][j]
return [Link]()
# Example
matrix1 = [[1, 2], [3, 4]]
matrix2 = [[5, 6], [7, 8]]
result = add_matrices(matrix1, matrix2)
print("Added matrix:")
for row in result:
print(row)
Multiply Two Matrices
import numpy as np
def multiply_matrices(matrix1, matrix2):
rows1 = len(matrix1)
cols1 = len(matrix1[0])
cols2 = len(matrix2[0])
result = [Link]((rows1, cols2), dtype=int)
for i in range(rows1):
for j in range(cols2):
for k in range(cols1):
result[i][j] += matrix1[i][k] * matrix2[k][j]
return [Link]()
# Example
matrix1 = [[1, 2], [3, 4]]
matrix2 = [[5, 6], [7, 8]]
result = multiply_matrices(matrix1, matrix2)
print("Multiplied matrix:")
for row in result:
print(row)