Python matrix sum 2x2

Asked

Viewed 249 times

0


m =  []
for l in range(0, 2):
    m.append([])
    for c in range (0, 2):
        valores = int(input("Digite valores: "))
        m[l].append(valores)
print("-"*30)
print(m)
print("-"*30)

for l in range(0, 2):
    print("|", end="")
    for c in range(0,2):
        print("{0:^5}".format(m[l][c]), end="")
    print("|")


m2 =  []
print("-"*30)
for l2 in range(0, 2):
    m2.append([])
    for c2 in range (0, 2):
        valores2 = int(input("Digite valores: "))
        m2[l2].append(valores2)
print("-"*30)
print(m2)
print("-"*30)

for l2 in range(0, 2):
    print("|", end="")
    for c2 in range(0,2):
        print("{0:^5}".format(m2[l2][c2]), end="")
    print("|")
print("-"*30)
m3 = []

soma  = m[0][0] + m[0][1]          #Somando valores da primeira matriz 
soma2 = m[1][0] + m[1][1]           #...

for l3 in range (0, 1):
        m3.append([])
        for c3 in range(0, 1):
                m3[l3].append(soma)
                m3[l3].append(soma2)

print(m3)

print("-"*30)

m4 = []

soma3 = m2[0][0] + m2[0][1]
soma4 = m2[1][0] + m2[1][1]

for l4 in range (0,1):
        m4.append([])
        for c4 in range(0, 1):
              m4[l4].append(soma3)
              m4[l4].append(soma4)  

print(m4)
print("-"*30)

mr = m3 + m4

print(mr)

for l5 in range(0, 2):
    print("|", end="")
    for c5 in range(0,2):
        print("{0:^5}".format(mr[l5][c5]), end="")
    print("|")
print("-"*30)

"Create a 2x2 matrix and add the two" As the code shows, I created two matrices, but I’m not able to make the sum of them. I created an array to store, but it’s printing the matrix at the different position.

  • The idea of defining a total matrix seems plausible to me. How did the code for this and what errors got?

  • So I made a matrix to do this, but when I compiled it stored the sum of the lines of the first matrix only and ended up ignoring the second matrix.

  • Could add the code to the question?

  • One moment, I’m redoing the lines here, so I’ll finish posting here.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.