Python Forum
Transposing a Matrix WITHOUT numpy
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Transposing a Matrix WITHOUT numpy
#1
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.")
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(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.
Reply
#4
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
(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...
Reply
#6
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Matrix Operations Without Numpy or Incorporating Python into Webpage ebryski 1 104 11 hours ago
Last Post: jefsummers
  Transposing Table salihozturk 3 908 Mar-03-2019, 06:55 PM
Last Post: salihozturk
  Transposing in Excel and converting to txt muhsin 2 1,438 Dec-10-2017, 06:06 PM
Last Post: muhsin
  matrix from matrix python numpy array shei7141 1 1,845 Jan-16-2017, 06:10 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020