questionet
임시 본문
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def matrix_multiply(arr1, arr2): | |
# Get the dimensions of the matrices | |
rows_arr1, cols_arr1 = len(arr1), len(arr1[0]) | |
rows_arr2, cols_arr2 = len(arr2), len(arr2[0]) | |
# Check if the matrices can be multiplied | |
if cols_arr1 != rows_arr2: | |
raise ValueError("Number of columns in arr1 must be equal to the number of rows in arr2") | |
# Initialize the result matrix with zeros | |
result = [[0 for _ in range(cols_arr2)] for _ in range(rows_arr1)] | |
# Transpose the second matrix for easier iteration | |
arr2_transposed = list(map(list, zip(*arr2))) | |
# Perform matrix multiplication | |
for i in range(rows_arr1): | |
for j in range(cols_arr2): | |
result[i][j] = sum(x * y for x, y in zip(arr1[i], arr2_transposed[j])) | |
return result |