Hasan Chr - 1 year ago 85
Python Question

# N×M matrix multiplication for python

i am actually writing a code to actually perform matrix multiplication on a n×m matrix
the closest that i got is the following

``````X = [[15,2,9],
[1 ,3,4],
[7 ,2,9]]

Y = [[5,8,1,2],
[6,7,3,0],
[4,5,9,1]]

result = [[0,0,0,0],
[0,0,0,0],
[0,0,0,0]]

for i in range(len(X)):
for j in range(len(Y[0])):
for k in range(len(Y)):
result[i][j] += X[i][k] * Y[k][j]

for numbs in result:
print(numbs)
``````

However i cannot seem to find a way to actually perform a n×m multiplication
and its limited to the size of my lists,

how can i allow the user to decide what is the dimension and allow him to input the matrix that is as big as he wants with one condition that the two matrices are n×m m×n

Your question seems to be unclear, although I'm pretty sure there are duplicates.

If your question was to perform multiplication of two dimensional array, here is how to create n x m or m x n matrix:

``````n = int(input())
m = int(input())

matrix = [[0 for i in range(m) for j in range(n)]
``````

For entering the values,

``````for i in range(n):
for j in range(m):
l[i][j] = input()
``````

Then, you may use the rest of your code to calculate the resulting array.

And for displaying the elements of the resulting matrix,

``````for i in range(n):
for j in range(m):
print l[i][j],
print
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download