Mar-01-2019, 10:41 PM
(This post was last modified: Mar-01-2019, 10:41 PM by TreasureDragon.)
So our assignment is to ask for user input for two matrices and then multiply them and show the result then switch the result to a transpose. Since I did it in 1D instead of 2D, I have to transpose the original A and B rather than C. So how can I do this? This is what I have so far and all of this works as intended. Just need help with the transposing part:
Ar, Ac = input("Enter matrix A rows and columns: ").split()
Br, Bc = input("Enter matrix B rows and columns: ").split()
A = input("Enter matrix A: ").split()
B = input("Enter matrix B: ").split()
intAr, intAc = int(Ar), int(Ac)
intBr, intBc = int(Br), int(Bc)
AMatrix = list()
BMatrix = list()
CMatrix = list()
if Ac == Br:
print(f"\nMatrix A:")
for x in range(intAr):
AList = list()
for y in range(intAc):
AList.append(int(A[y + intAc*x]))
print(str(A[y+intAc*x])," ", end = '')
AMatrix.append(AList)
print()
print(f"\nMatrix B:")
for x in range(intBr):
BList = list()
for y in range(intBc):
BList.append(int(B[y + intBc*x]))
print(str(B[y+intBc*x])," ", end = '')
BMatrix.append(BList)
print()
print(f"\nMatrix C:")
for x in range(intAr):
for y in range(intBc):
C = 0
for z in range(intBr):
C += (AMatrix[x][z] * BMatrix[z][y])
CMatrix.append(C)
print(C, ' ', end ='')
print()
print(f"\nTranspose matrix T:")
else:
print(f"\nCannot be multiplied. Please make sure the number of columns of A and rows of B are the same.")
