Matrix (Create 2 Matrices and Sum them)

Asked

Viewed 7,755 times

1

How could I build this matrix without using Random/randit? Then I need to do the function sum element by element, of the first two. The sum shall be settled via written sub-programme for that purpose. This subprogram should receive as parameters only the two input matrices and return the resulting matrix if it is possible to evaluate the sum, or None, if it is not possible. After the return of the subprogram, the main program must display the content of the matrix following the format presented in the matrix construction. If None is returned, the main program should send the message "Cannot add matrices of different dimensions".

#Contrução da Matriz 1 e Matriz 2 - Programa Principal
import random
matriz1 = []
n = int(input("Informe a quantidade de linhas\n da matriz 1:" ))
m = int(input("Informe a quantidade de colunas\n da matriz 1:" ))
for i in range(n):
     matriz1.append([])
     for j in range(m):
        matriz1[i].append(random.randint(0,100))

for i in range(len(matriz1)):
    for j in range(len(matriz1[i])):
        print(matriz1[i][j], end=" ")
    print ("\n")


matriz2 = []
n = int(input("Informe a quantidade de linhas\n da matriz 2:" ))
m = int(input("Informe a quantidade de colunas\n da matriz 2:" ))

for i in range(n):
     matriz2.append([])
     for j in range(m):
        matriz2[i].append(random.randint(0,100))

for i in range(len(matriz2)):
    for j in range(len(matriz2[i])):
        print(matriz2[i][j], end=" ")
    print ("\n")
  • Do you need to do this with the same result but without using the Random function? That’s the only change to do?

  • yes.... remove the Random/randit...... and make the sum function that I am not getting... def somarMatrizes(matriz1, matriz2): if Len(matriz1)!= Len(matriz2): print ("It is not possible to add matrices of different dimensions") Return None matriz_soma = [] for i in range(Len(matriz1)): matriz_soma.append([]) for j in range(Len(matriz1)): matriz_soma[i]. append( matriz1[i][j] + matriz2[i][j]) Return matriz_soma print (somarMarines)

  • @ Miguel ...... Could you help me with this too? Continuing what I did? def somarMatrizes(matriz1, matriz2): if Len(matriz1)!= Len(matriz2): print ("It is not possible to add matrices of different dimensions") Return None matriz_soma = [] for i in range(Len(matriz1)): matriz_soma.append([]) for j in range(Len(matriz1)): matriz_soma[i]. append( matriz1[i][j] + matriz2[i][j]) Return matriz_soma print (somarMatrizes)

1 answer

0


To add up the matrices can do:

def somarMatrizes(matriz1, matriz2):
    if(len(matriz1) != len(matriz2) or len(matriz1[0]) != len(matriz2[0])):
        return None
    result = []
    for i in range(len(matriz1)):   
        result.append([])
        for j in range(len(matriz1[0])):
            result[i].append(matriz1[i][j] + matriz2[i][j])
    return result

To make use of this in the last line of the code can do:

soma = somarMatrizes(matriz1, matriz2) # soma e o nosso retorno, result
if soma is not None:
    for i in soma:
        print(i)
else:
    print('Matrizes devem conter o mesmo numero de linhas e colunas')
  • print sera print(summeMatrizes, end=" ") ?

  • Sorry, I’ll make an addition

  • Muitissimo Obg!!!!!

  • No @Absoares. I think I should change the title so that the solution of the problem did not involve the non-use of Random. And if the answer really helped you can accept it, to the left of the answer, under the down arrow

  • I understand, I’ll change accordingly

  • Edited by @Absoares

  • Yes, but the second part of the answer is no longer within the method (function) @Absoares

  • OK got it! Vlw!!!!!!!! @Miguel

  • I’m glad you solved @Absoares. I suggest you take a look here, and you get a reputation for seeing this through to the end: http://answall.com/tour

Show 4 more comments

Browser other questions tagged

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