Python Forum

Full Version: Transposing a Matrix WITHOUT numpy
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.")
It's incredibly simple to transpose a 2D matrix in Python:

transposed = zip(*matrix)
It's so simple, that if you are working in 1D, I would suggest converting to 2D to do the transposition.
(Mar-01-2019, 10:46 PM)ichabod801 Wrote: [ -> ]It's incredibly simple to transpose a 2D matrix in Python:

transposed = zip(*matrix)
It's so simple, that if you are working in 1D, I would suggest converting to 2D to do the transposition.

How can I convert the C Matrix into a 2D Matrix though? Sorry, this is my first semester ever in compsci so I am so lost right now.
Well, looking at your code, you are actually working in 2D. Your matrices are stored as a list of lists. So you can just use the code I showed you. Note that it will give you a generator, not a list, but you can fix that by doing transposed = list(zip(*matrix))

The reason it works is that zip takes any number of lists as parameters. It then returns a list of the first item in each list, a list of the second item in each list, a list of the third item in each list, and so on. The star (*) notation converts a sequence (like a list) into a series of parameters.
(Mar-02-2019, 06:55 PM)ichabod801 Wrote: [ -> ]Well, looking at your code, you are actually working in 2D. Your matrices are stored as a list of lists. So you can just use the code I showed you. Note that it will give you a generator, not a list, but you can fix that by doing transposed = list(zip(*matrix))

The reason it works is that zip takes any number of lists as parameters. It then returns a list of the first item in each list, a list of the second item in each list, a list of the third item in each list, and so on. The star (*) notation converts a sequence (like a list) into a series of parameters.

Oh. So just simply use the code? But it's a library function isn't it... We aren't allowed to use those for now :(
And the code didn't work...
It's a built-in function, just like list, int, range, and input.

You could write a set of loops to do what I described: get a list of the first item in each list, a list of the second item in each list, a list of the third item in each list, and so on. But you would have to loop through the indexes of the list and the sub-lists, which wouldn't be very Pythonic.