Skip to content

Instantly share code, notes, and snippets.

@luizaugustomm
Created October 12, 2011 20:06
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save luizaugustomm/1282374 to your computer and use it in GitHub Desktop.
Save luizaugustomm/1282374 to your computer and use it in GitHub Desktop.
Operações com matrizes em Python
#!/usr/bin/env python3
def somar(m1, m2):
matriz_soma = []
# Supondo que as duas matrizes possuem o mesmo tamanho
quant_linhas = len(m1) # Conta quantas linhas existem
quant_colunas = len(m1[0]) # Conta quantos elementos têm em uma linha
for i in range(quant_linhas):
# Cria uma nova linha na matriz_soma
matriz_soma.append([])
for j in range(quant_colunas):
# Somando os elementos que possuem o mesmo índice
soma = m1[i][j] + m2[i][j]
matriz_soma[i].append(soma)
return matriz_soma
def mult_escalar(matriz, escalar):
matriz_mult = []
quant_linhas = len(matriz) # Conta quantas linhas existem
quant_colunas = len(matriz[0]) # Conta quantos elementos têm em uma linha
for i in range(quant_linhas):
# Cria uma nova linha na matriz_mult
matriz_mult.append([])
for j in range(quant_colunas):
# Multiplicando cada elemento pelo escalar
mult = escalar * matriz[i][j]
matriz_mult[i].append(mult)
return matriz_mult
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment