python的N×M矩阵乘法

时间:2016-07-14 19:26:56

标签: python matrix

我实际上正在编写一个代码来实际在n×m矩阵上执行矩阵乘法 我得到的最接近的是

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)

然而,我似乎找不到实际执行n×m乘法的方法 它仅限于我的清单大小,

我如何允许用户决定维度是什么,并允许他输入两个矩阵为n×m m×n

的一个条件,尽可能大的矩阵

3 个答案:

答案 0 :(得分:0)

你的问题似乎不太清楚,虽然我很确定有重复的问题。

如果你的问题是执行二维数组的乘法,这里是如何创建n x m或m x n矩阵:

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

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

输入值

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

然后,您可以使用其余代码来计算生成的数组。

用于显示结果矩阵的元素,

for i in range(n):
    for j in range(m):
        print l[i][j],
    print

答案 1 :(得分:0)

大家好,感谢您的帮助,我实际上能够解决我的问题,而且我发布了我的解决方案,每个人都可以从中受益,因为代码完全可以让任何人使用它,你可以看到很多打印发生但这只是为了说明一切

再次感谢您

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

答案 2 :(得分:0)

你应该觉得这很有用:

Run it online in REPL或...... 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

运行时,它可能如下所示:

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]