WRITE CODE IN PYTHON PLEASE thank you. Make a function to multiply n x n square
ID: 3689282 • Letter: W
Question
WRITE CODE IN PYTHON PLEASE thank you.
Make a function to multiply n x n square matrices. Input parameters: 2 square matrices (2 nested lists) Returned output: 1 square matrix (1 nested list) Your function should detect the size of input matrices automatically. Assume that users of your function input normal square matrices of the same size, so you don't have to handle exceptions. Make an example to use your function. Print input and output matrices in the square (matrix) form (not the list form).Explanation / Answer
X = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]
# 3x4 matrix
Y = [[5,8,1,2],
[6,7,3,0],
[4,5,9,1]]
# result is 3x4
result = [[0,0,0,0],
[0,0,0,0],
[0,0,0,0]]
# iterate through rows of X
for i in range(len(X)):
# iterate through columns of Y
for j in range(len(Y[0])):
# iterate through rows of Y
for k in range(len(Y)):
result[i][j] += X[i][k] * Y[k][j]
for r in result:
print(r)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.