Skip to content

Instantly share code, notes, and snippets.

@hamzaavvan
Created October 6, 2019 09:48
Show Gist options
  • Save hamzaavvan/1b75a578e4bbd2dfdc8df1a0e0816475 to your computer and use it in GitHub Desktop.
Save hamzaavvan/1b75a578e4bbd2dfdc8df1a0e0816475 to your computer and use it in GitHub Desktop.
Python - Matrix Addition/Subtraction
def createMatrix():
_matrix = []
ms=int(input("Enter no. of matrices to create: "))
for i in range(ms):
print("\nMatrix %i"%(i+1))
r=int(input("\tEnter no. of rows: "))
c=int(input("\tEnter no. of columns: "))
m=[]
b=[]
for i in range(r):
for j in range(c):
v= int(input("\tEnter value for row "+str(i)+" column "+str(j)+": "))
b.append(v)
m.append(b)
b=[]
_matrix.append(m)
return _matrix
matrix = createMatrix()
# Sample 2 Matrices assigning to m1 & m2
m1 = matrix[0]
m2 = matrix[1]
c=[]
for i in range(len(m1)):
if (len(m1) != len(m2)) or (len(m1[i]) != len(m2[i])):
print("Not possible")
break
for j in range(len(m1[i])):
c.append(m1[i][j]+m2[i][j])
print(c)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment