Avi
Avi

Reputation: 2283

Basic matrix transpose in python

I tried the most basic approach for matrix transpose in python. However, I don't get the required results. Following by the code:

A = [ [1, 1, 1, 1], 
    [2, 2, 2, 2], 
    [3, 3, 3, 3], 
    [4, 4, 4, 4]] 

#print (A)
def TS (A):
    B = A
    for i in (range(len(A))):
        for j in (range(len(A))):
            A[i][j] = B [j][i]
TS(A)
#print (A)

for i in range(len(A)): 
    for j in range(len(A)): 
        print(B[i][j], " ", end='') 
    print() 

This is the result I get:

1  2  3  4  
2  2  3  4  
3  3  3  4  
4  4  4  4  

Upvotes: 1

Views: 547

Answers (5)

jose_bacoy
jose_bacoy

Reputation: 12684

Copy A to B using deepcopy then it should be B [i][j] = A [j][i]. Must be a typo error.

A = [[1, 1, 1, 1], 
    [2, 2, 2, 2], 
    [3, 3, 3, 3], 
    [4, 4, 4, 4]] 

#print (A)
def TS (A):
    from copy import deepcopy
    B = deepcopy(A)
    for i in (range(len(A))):
        for j in (range(len(A))):
            B[i][j] = A [j][i]
    return B
B = TS(A)
#print (len(A))

for i in range(len(B)): 
    for j in range(len(B)): 
        print(B[i][j], " ", end='') 
    print() 

Result:

1  2  3  4  
1  2  3  4  
1  2  3  4  
1  2  3  4 

Upvotes: 1

Naazneen Jatu
Naazneen Jatu

Reputation: 546

B was a label on matrix A, that is every modification to A, also modified B. Hence the wrong values from second row. Why don't you try it like this...

 A = [ [1, 1, 1, 1], 
 [2, 2, 2, 2], 
 [3, 3, 3, 3], 
 [4, 4, 4, 4]] 
 def TS(A):
    for i in range(len(A)): 
        for j in range(len(A)): 
            print(A[j][i], " ", end='') 
        print()        
 TS(A)

Upvotes: 0

GOVIND DIXIT
GOVIND DIXIT

Reputation: 1828

A = [ [1, 1, 1, 1], 
    [2, 2, 2, 2], 
    [3, 3, 3, 3], 
    [4, 4, 4, 4]]

def transpose(A,B): 

    for i in range(len(A)): 
        for j in range(len(A)): 
            B[i][j] = A[j][i]    

B = [[0 for x in range(len(A))] for y in range(len(A))]  

transpose(A, B) 

print("Result matrix is") 
for i in range(len(A)): 
    for j in range(len(A)): 
        print(B[i][j], " ", end='') 
    print() 

Output

Result matrix is

1  2  3  4
1  2  3  4
1  2  3  4
1  2  3  4

Upvotes: 1

Reblochon Masque
Reblochon Masque

Reputation: 36662

Your problem is two fold:

1- B was a label on matrix A, that is every modification to A, also modified B
2- B was local to the transpose function, and could not be accessed outside

A = [[1, 1, 1, 1], 
     [2, 2, 2, 2], 
     [3, 3, 3, 3], 
     [4, 4, 4, 4]] 

def TS (A):
    B = [row[:] for row in A]   # make a copy of A, not assigning a new label on it.
    for i in (range(len(A))):
        for j in (range(len(A))):
            B[i][j] = A[j][i]
    return B

B = TS(A)

for i in range(len(A)): 
    for j in range(len(A)): 
        print(B[i][j], " ", end='') 
    print() 

output:

1  2  3  4  
1  2  3  4  
1  2  3  4  
1  2  3  4 

Upvotes: 1

Raveen Beemsingh
Raveen Beemsingh

Reputation: 554

why do dont you try numpy :)

import numpy as np
z = np.transpose(np.array(A))

Upvotes: 3

Related Questions