lundi 11 février 2019

Adding a n extra colum from another matrix

I have these two matrix:

 A = [[2, 7, 3, 6], [3, 3, 4, 4], [6, 9, 5, 3], [4, 2, 1, 7]]
 B = [[2, 6, 3, 5], [-1, 2, -3, 1], [2, -5, 7, 3]]

I need to create three matrices A+1st column of B, and so on

So I need this final result:

 A1=[[2, 7, 3, 6, 2], [3, 3, 4, 4, 6], [6, 9, 5, 3, 3], [4, 2, 1, 7, 5]]
 A2=[[2, 7, 3, 6, -1], [3, 3, 4, 4, 2], [6, 9, 5, 3, -3], [4, 2, 1, 7, 7]]
 A3=[[2, 7, 3, 6, 2], [3, 3, 4, 4, -5], [6, 9, 5, 3, 7], [4, 2, 1, 7, 3]]

I start doing the following code, but I only got one of those

(INPUT)

for j in range(len(B)):    

    for i in range(j):

        b = B[j][i]
        A = [x + [b] for x in A]
    print(A)

(OUTPUT)

[[2, 7, 3, 6], [3, 3, 4, 4], [6, 9, 5, 3], [4, 2, 1, 7]]
[[2, 7, 3, 6, -1], [3, 3, 4, 4, -1], [6, 9, 5, 3, -1], [4, 2, 1, 7, -1]]
[[2, 7, 3, 6, -1, 2, -5], [3, 3, 4, 4, -1, 2, -5], [6, 9, 5, 3, -1, 2, -5], [4, 2, 1, 7, -1, 2, -5]]

Process finished with exit code 0

Aucun commentaire:

Enregistrer un commentaire