0% found this document useful (0 votes)
11 views3 pages

Matrix Operations: Addition, Transpose, Multiply

The document provides Python functions for basic matrix operations including addition, transposition, and multiplication. Each function is accompanied by example usage demonstrating how to perform the operations on sample matrices. The code includes error handling for matrix multiplication to ensure compatibility of dimensions.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

Matrix Operations: Addition, Transpose, Multiply

The document provides Python functions for basic matrix operations including addition, transposition, and multiplication. Each function is accompanied by example usage demonstrating how to perform the operations on sample matrices. The code includes error handling for matrix multiplication to ensure compatibility of dimensions.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

addition of two matrix

def add_matrices(matrix1, matrix2):

rows = len(matrix1)

cols = len(matrix1[0])

result = [[0 for _ in range(cols)] for _ in range(rows)]

for i in range(rows):

for j in range(cols):

result[i][j] = matrix1[i][j] + matrix2[i][j]

return result

# Example usage:

matrix_a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

matrix_b = [[9, 8, 7], [6, 5, 4], [3, 2, 1]]

sum_matrix = add_matrices(matrix_a, matrix_b)

print("Matrix Addition:")

for row in sum_matrix:

print(row)

2. Transpose

def transpose_matrix(matrix):

"""

Transposes a matrix and returns the result.


"""

rows = len(matrix)

cols = len(matrix[0])

result = [[0 for _ in range(rows)] for _ in range(cols)]

for i in range(rows):

for j in range(cols):

result[j][i] = matrix[i][j]

return result

# Example usage:

original_matrix = [[1, 2, 3], [4, 5, 6]]

transposed_matrix = transpose_matrix(original_matrix)

print("\nMatrix Transpose:")

for row in transposed_matrix:

print(row)

3. Multiplication

def multiply_matrices(matrix1, matrix2):

"""

Multiplies two matrices and returns the result.

Requires the number of columns in matrix1 to equal the number of rows in matrix2.

"""

rows1 = len(matrix1)

cols1 = len(matrix1[0])
rows2 = len(matrix2)

cols2 = len(matrix2[0])

if cols1 != rows2:

raise ValueError("Number of columns in the first matrix must equal the number of rows
in the second matrix for multiplication.")

result = [[0 for _ in range(cols2)] for _ in range(rows1)]

for i in range(rows1):

for j in range(cols2):

for k in range(cols1): # or rows2

result[i][j] += matrix1[i][k] * matrix2[k][j]

return result

# Example usage:

matrix_x = [[1, 2], [3, 4]]

matrix_y = [[5, 6], [7, 8]]

product_matrix = multiply_matrices(matrix_x, matrix_y)

print("\nMatrix Multiplication:")

for row in product_matrix:

print(row)

You might also like