linear-algebra

矩阵乘法:附解题示例的分步指南

矩阵乘法究竟是怎么运作的——维度规则、行乘列的算法、常见错误,以及它与线性映射的联系。
AI-Math Editorial Team

作者: AI-Math Editorial Team

发布于 2026-05-01

矩阵乘法是驱动线性代数、计算机图形学、机器学习和物理仿真的运算。然而大多数学生只是把它当作一套机械的算法来学,从未看到它为什么要这样定义。本指南同时给你算法直觉。

先看维度规则

在计算任何东西之前,先检查维度。要做 ABA \cdot B

  • AA 的形状必须是 m×nm \times n
  • BB 的形状必须是 n×pn \times p
  • 结果 ABAB 的形状为 m×pm \times p

内侧维度必须匹配n=nn = n);外侧维度成为结果的形状。

如果你试图用一个 3×43 \times 4 乘以一个 5×25 \times 2,这个运算就是未定义的——再多的算术也救不了你。

行乘列的算法

ABAB(i,j)(i, j) 元素是 AA 的第 ii 行与 BB 的第 jj 列的点积

(AB)ij=k=1nAikBkj(AB)_{ij} = \sum_{k=1}^{n} A_{ik} B_{kj}

解题示例

A=(1234),B=(5678)A = \begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix}, \quad B = \begin{pmatrix} 5 & 6 \\ 7 & 8 \end{pmatrix}

计算 ABAB

  • (AB)11=15+27=19(AB)_{11} = 1\cdot 5 + 2\cdot 7 = 19
  • (AB)12=16+28=22(AB)_{12} = 1\cdot 6 + 2\cdot 8 = 22
  • (AB)21=35+47=43(AB)_{21} = 3\cdot 5 + 4\cdot 7 = 43
  • (AB)22=36+48=50(AB)_{22} = 3\cdot 6 + 4\cdot 8 = 50

所以 AB=(19224350)AB = \begin{pmatrix} 19 & 22 \\ 43 & 50 \end{pmatrix}

为什么乘法这样定义?

矩阵表示向量空间之间的线性映射。如果 AARn\mathbb{R}^n 映到 Rm\mathbb{R}^mBBRp\mathbb{R}^p 映到 Rn\mathbb{R}^n,那么 ABAB 应当是这些映射的复合。行乘列规则恰恰就是产生复合的方式。这个算法并非任意——它是由"先施加 BB,再施加 AA"必须被 ABAB 编码这一要求推导出来的。

性质(以及非性质!)

性质是否成立?
A(BC)=(AB)CA(BC) = (AB)C 结合律
A(B+C)=AB+ACA(B + C) = AB + AC 分配律
AB=BAAB = BA 交换律一般不成立
AB=0A=0AB = 0 \Rightarrow A = 0B=0B = 0

不可交换性是相对于标量算术需要做的最大思维调整。

常见错误

  • 把行列乘积相加而不是相乘(你两者都要做——先逐对相乘再求和)。
  • 颠倒维度检查的顺序——必须是 (m×n)(n×p)(m \times n)(n \times p),而不是 (n×m)(n×p)(n \times m)(n \times p)
  • 假定可交换性——即使 BABA 有定义,ABAB 也可能根本没有定义。

用 AI 矩阵求解器试一试

把任意一对矩阵输入矩阵计算器,即可看到完整的逐行演算过程。

相关参考:

常见问题

To multiply matrix A (m×n) by matrix B (n×p), the entry at row i, column j of the result is the dot product of row i of A and column j of B. The result is an m×p matrix. The inner dimensions must match: columns of A must equal rows of B.

No. In general AB ≠ BA. Matrix multiplication is associative (A(BC) = (AB)C) and distributive over addition, but not commutative. This is one of the fundamental differences between matrix algebra and scalar arithmetic.

The identity matrix I is a square matrix with 1s on the main diagonal and 0s everywhere else. It satisfies AI = IA = A for any compatible matrix A, playing the same role as the number 1 in scalar multiplication.

AI-Math Editorial Team

作者: AI-Math Editorial Team

发布于 2026-05-01

A small team of engineers, mathematicians, and educators behind AI-Math, focused on making step-by-step math help accessible to every student.