questionet

임시 본문

카테고리 없음

임시

orthanc 2023. 11. 13. 18:37
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
view raw dot_product.py hosted with ❤ by GitHub
Comments