1

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

Hasan Chr
  • 13
  • 4

3 Answers3

0

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
Amal Rajan
  • 153
  • 1
  • 11
0

Hello guys thank you for your help i was actually able to solve my problem, and i am posting my solution for everyone to benefit from it, Since the code is fully ready for anyone to use it you can see lots of printing happening but that is just to illustrate everything

Thank you again

print 'enter the dimensions of the first matrix'

print 'nbr of rows for matrix one '
a= input()

print 'nbr of columns for matrix one '
b=input()

print 'fill in the numbers of the first matrix '

Matrixone = [[0 for y in xrange(b)] for x in xrange(a)]
MatrixOneFilled= [[0 for y in xrange(b)] for x in xrange(a)]
for i in range(len(Matrixone)):
   for j in range(len(Matrixone[0])):
       MatrixOneFilled[i][j]= input()

print MatrixOneFilled

print 'enter the dimensions of the second matrix'
print 'nbr of rows for matrix two  '
c= input()

print 'nbr of Colums for matrix two '
d=input()

print 'fill in the numbers of the second matrix '
Matrixtwo = [[0 for y in xrange(d)] for x in xrange(c)]
MatrixTwoFilled= [[0 for y in xrange(d)] for x in xrange(c)]
for i in range(len(Matrixtwo)):
   for j in range(len(Matrixtwo[0])):
       MatrixTwoFilled[i][j]= input()
print MatrixTwoFilled

ResultMatrix = [[0 for y in xrange(d)] for x in xrange(a)]

for i in range(len(MatrixOneFilled)):
   for j in range(len(MatrixTwoFilled[0])):
       for k in range(len(MatrixTwoFilled)):
           ResultMatrix[i][j] += MatrixOneFilled[i][k] * MatrixTwoFilled[k][j]

print "Your Result Matrix is"
print ResultMatrix
Hasan Chr
  • 13
  • 4
  • If you want, I created some code that allows you to enter an entire row at once. I'll post in an answer. – mbomb007 Jul 14 '16 at 20:37
0

You should find this useful:

Run it online in REPL or... Run it online in Ideone

x,y = input("Enter the dimensions as ROWS,COLS:\n")
print

m1 = [[0 for i in xrange(y)] for j in xrange(x)] # X*Y
m2 = [[0 for i in xrange(x)] for j in xrange(y)] # Y*X
R  = [[0 for i in xrange(x)] for j in xrange(x)] # Result as X*X

def enter(m, n, rows, cols):
    print "Enter Matrix %d with height %d, width %d one row at a time...\n" % (n,rows,cols)

    for i in xrange(rows):
        m[i] = input("Row %d:" % (i+1)) # input a row like this: [1,2,3,...]
    print

enter(m1,1,x,y) # enter Matrix 1
enter(m2,2,y,x) # enter Matrix 2

for i in xrange(x):
    for j in xrange(x):
        R[i][j] = sum(m1[i][k] * m2[k][j] for k in xrange(y))

for row in R:
    print row

When it is run, it may look like this:

Enter the dimensions as ROWS,COLS:
2,1

Enter Matrix 1 with height 2, width 1 one row at a time...

Row 1:[1]
Row 2:[3]

Enter Matrix 2 with height 1, width 2 one row at a time...

Row 1:[4,-2]

[4, -2]
[12, -6]
mbomb007
  • 3,788
  • 3
  • 39
  • 68