0
What is simplest way to implement a user defined matrix?
Give a code for addition if possible
1 Resposta
+ 3
matrix is a two-dimension array. you can use nested lists (list of lists) to simulate that.
matrix = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]
this will be a matrix:
123
456
789
you can refer to elements with double index, like
matrix[1][2] #returns 6 (row 1, col 2, coutning from zero, of course)
if you need many matrixes like that, you can define a function to create it, or even a class.
def make_matrix(rows, cols):
ans = list()
for row in range(rows):
ans.append(list())
for col in range(cols):
ans[row].append(0)
return ans
that function will return a new matrix of specified size filled with zeros. you can create as much as you want:
a = make_matrix(2, 4)
b = make_matrix(5, 5)
and so on.
also there is a NumPy module that provides a bunch of mathematical things to use in python, including matrix and functions related to them.