[英]How to multiply each row in a matrix by each column in the second matrix in Python?
I know the steps to multiply two matrices are as follow 我知道将两个矩阵相乘的步骤如下
Step 1: Make sure that the the number of columns in the 1st one equals the number of rows in the 2nd one. 步骤1:确保第一个中的列数等于第二个中的行数。
Step 2: Multiply the elements of each row of the first matrix by the elements of each column in the second matrix. 步骤2:将第一矩阵的每一行的元素乘以第二矩阵中每列的元素。
Step 3: Add the products. 第3步:添加产品。
How do you do the second step? 你如何做第二步?
For example 例如
A = [[3,4,5],[5,0,6],[5,7,1]]
B = [[2,1,3],[2,6,4]]
So far I got a function to find each column for the second one 到目前为止,我有一个函数来查找第二列的每一列
def column(B,j):
col = []
for column in B:
col.append(column[j])
return col
Next I will have to make a function that finds each row for the first one 接下来,我将创建一个函数,找到第一行的每一行
def rows(A,i):
But then I don't know how to create a function that will multiply them together like 但后来我不知道如何创建一个将它们组合在一起的函数
row(A,0) • col(B,0)
row(A,0) • col(B,1)
row(A,1) • col(B,0)
row(A,1) • col(B,1)
row(A,2) • col(B,0)
row(A,2) • col(B,1)
You should probably use numpy: 你应该使用numpy:
import numpy as np
np.dot(row(A,0), col(B,0))
However, assuming you don't want to use that, you could do: 但是,假设您不想使用它,您可以:
def dot(arr1, arr2):
return sum([x*y for x,y in zip(arr1, arr2)])
dot(row(A,0), col(B,0))
If you insist on using lists for this.... 如果你坚持使用这个列表....
For C = AB, you need 对于C = AB,您需要
C_{ij} = sum(A_{ik} * B_{kj}) C_ {ij} = sum(A_ {ik} * B_ {kj})
Here, i, j, and k are subscripts, with the first subscript denoting the row and the second denoting the column. 这里,i,j和k是下标,第一个下标表示行,第二个下标表示列。 i, j, k run over the rows and columns (ie, list indices) of the matrix, so you can just write for loops over i, j, and k.
i,j,k遍历矩阵的行和列(即列表索引),因此您只需在i,j和k上写入循环。
A has 3 columns, while B has 2 rows. A有3列,B有2列。 So your example seems to contradict the requirement stated in Step 1. Nevertheless, this might be close to what you are looking for.
因此,您的示例似乎与步骤1中所述的要求相矛盾。然而,这可能接近您所寻找的要求。
In [1]: A = [[3,4,5],[5,0,6],[5,7,1]]
In [2]: B = [[2,1,3],[2,6,4]]
In [3]: [[sum(r*c for r,c in zip(row, col)) for col in B] for row in A]
Out[3]: [[25, 50], [28, 34], [20, 56]]
By the way, here is a useful trick which you might find useful: If you want to transpose a matrix, use zip(*B)
: 顺便说一句,这是一个有用的技巧,你可能会觉得有用:如果你想转置矩阵,请使用
zip(*B)
:
In [4]: zip(*B)
Out[4]: [(2, 2), (1, 6), (3, 4)]
This may be useful to you because it allows you to easily loop through the columns of B
. 这可能对您有用,因为它允许您轻松遍历
B
列。
Here's a worked out example: 这是一个成功的例子:
>>> from pprint import pprint
>>> def mmul(A, B):
nr_a, nc_a = len(A), len(A[0])
nr_b, nc_b = len(B), len(B[0])
if nc_a != nr_b:
raise ValueError('Mismatched rows and columns')
return [[sum(A[i][k] * B[k][j] for k in range(nc_a))
for j in range(nc_b)] for i in range(nr_a)]
>>> A = [[1, 2, 3, 4]]
>>> B = [[1],
[2],
[3],
[4]]
>>> pprint(mmul(A, B))
[[30]]
>>> pprint(mmul(B, A), width=20)
[[1, 2, 3, 4],
[2, 4, 6, 8],
[3, 6, 9, 12],
[4, 8, 12, 16]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.