简体   繁体   English

如何将Python中第二个矩阵中每列的矩阵中的每一行乘以?

[英]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.

相关问题 如何将矩阵的每一行与numpy相乘 - how to multiply each row of a matrix with numpy 在Numpy中将矩阵乘以另一个矩阵的每一行 - Multiply matrix by each row of another matrix in Numpy 矩阵乘法:用Python中的另一个2D矩阵乘以矩阵的每一行 - Matrix Multiplication: Multiply each row of matrix by another 2D matrix in Python 用不同的旋转矩阵乘以每一行 - Multiply each row by different rotation matrix 如何将矩阵与向量相乘,以便每一行都与该向量相乘 - How to multiply a matrix with vector such that each row is multiplied elementwise with this vector 如何将softmax的每一行乘以tensorflow中的特定矩阵? - How to multiply each row of the softmax by a specific matrix in tensorflow? 如何在张量流上将3D矩阵的每一行与另一个3D矩阵的每个元素相乘? - How to multiply each row of a 3D matrix with each element of another 3D matrix on tensorflow? 将Scipy中的csr矩阵列的每个元素相乘 - Multiply each element of a csr matrix column in Scipy 如何在Theano中将矩阵的每一列乘以矢量元素方式? - How to multiply each column of a matrix by a vector element-wise in Theano? 矩阵的每一行和每一列中的最小值-Python - Minimum value in each row and each column of a matrix - Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM