序号 | 内容 |
---|---|
1 | 【数理知识】向量数乘,内积,外积,matlab代码实现 |
2 | 【数理知识】矩阵普通乘积,哈达玛积,克罗内克积,点乘,点积,叉乘,matlab代码实现 |
首先介绍矩阵
1. 矩阵基本形式
在数学中,矩阵是一个按照长方阵列排列的复数或实数集合。由 m × n m \times n m×n个数 a i j a_{ij} aij 排成的 m m m 行 n n n 列的数表称为 m m m 行 n n n 列的矩阵,简称 m × n m \times n m×n 矩阵。记作:
A = [ a 11 a 12 ⋯ a 1 n a 21 a 22 ⋯ a 2 n ⋮ ⋮ ⋱ ⋮ a m 1 a m 2 ⋯ a m n ] A = \left[\begin{matrix} a_{11} & a_{12} & \cdots & a_{1n} \\ a_{21} & a_{22} & \cdots & a_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ a_{m1} & a_{m2} & \cdots & a_{mn} \\ \end{matrix}\right] A= a11a21⋮am1a12a22⋮am2⋯⋯⋱⋯a1na2n⋮amn
2. 矩阵基本运算 - 普通乘积,matmul product
矩阵相乘最重要的方法是一般矩阵乘积。它只有在第一个矩阵的列数(column)和第二个矩阵的行数(row)相同时才有意义。即线性代数学学的,左行乘以右列。
- 左边矩阵的列数必须与右边矩阵的行数相等
- 不满足交换律,即 A B ≠ B A ( B A 存在 ) AB \ne BA (BA 存在) AB=BA(BA存在)
A = [ a 11 a 12 a 13 a 21 a 22 a 23 ] 2 × 3 , B = [ b 11 b 12 b 13 b 14 b 21 b 22 b 23 b 24 b 31 b 32 b 33 b 34 ] 3 × 4 A = \left[\begin{matrix} \red{a_{11}} & \red{a_{12}} & \red{a_{13}} \\ a_{21} & a_{22} & a_{23} \\ \end{matrix}\right]_{\green{2 \times 3}}, \quad B = \left[\begin{matrix} \blue{b_{11}} & b_{12} & b_{13} & b_{14} \\ \blue{b_{21}} & b_{22} & b_{23} & b_{24} \\ \blue{b_{31}} & b_{32} & b_{33} & b_{34} \\ \end{matrix}\right]_{\green{3 \times 4}} A=[a11a21a12a22a13a23]2×3,B= b11b21b31b12b22b32b13b23b33b14b24b34 3×4
C = A 2 × 3 B 3 × 4 = C 2 × 4 = [ a 11 b 11 + a 12 b 21 + a 13 b 31 a 11 b 12 + a 12 b 22 + a 13 b 32 a 11 b 13 + a 12 b 23 + a 13 b 33 a 11 b 14 + a 12 b 24 + a 13 b 34 a 21 b 11 + a 22 b 21 + a 23 b 31 a 21 b 12 + a 22 b 22 + a 23 b 32 a 21 b 13 + a 22 b 23 + a 23 b 33 a 21 b 14 + a 22 b 24 + a 23 b 34 ] 2 × 4 \begin{aligned} C &= A_{\green{2 \times 3}} B_{\green{3 \times 4}} = C_{\green{2 \times 4}} \\ &=\left[\begin{matrix} \red{a_{11}} \blue{b_{11}} + \red{a_{12}} \blue{b_{21}} + \red{a_{13}} \blue{b_{31}} & \red{a_{11}} b_{12} + \red{a_{12}} b_{22} + \red{a_{13}} b_{32} & \red{a_{11}} b_{13} + \red{a_{12}} b_{23} + \red{a_{13}} b_{33} & \red{a_{11}} b_{14} + \red{a_{12}} b_{24} + \red{a_{13}} b_{34} \\ a_{21} \blue{b_{11}} + a_{22} \blue{b_{21}} + a_{23} \blue{b_{31}} & a_{21} b_{12} + a_{22} b_{22} + a_{23} b_{32} & a_{21} b_{13} + a_{22} b_{23} + a_{23} b_{33} & a_{21} b_{14} + a_{22} b_{24} + a_{23} b_{34} \\ \end{matrix}\right]_{\green{2 \times 4}} \end{aligned} C=A2×3B3×4=C2×4=[a11b11+a12b21+a13b31a21b11+a22b21+a23b31a11b12+a12b22+a13b32a21b12+a22b22+a23b32a11b13+a12b23+a13b33a21b13+a22b23+a23b33a11b14+a12b24+a13b34a21b14+a22b24+a23b34]2×4
A = [1 2 3;
4 5 6];
B = [1 2 3 4;
5 6 7 8;
9 10 11 12];
Matlab语法:C = A * B
>> C = A * B
ans =
38 44 50 56
83 98 113 128
>> A.*B
对于此运算,数组的大小不兼容。
>> dot(A, B)
错误使用 dot
A 和 B 的大小必须相同。
>> cross(A, B)
错误使用 cross
A 和 B 的大小必须相同。
3. 矩阵基本运算 - 哈达玛积 Hadamard product
当矩阵 A A A 和矩阵 B B B 的维度相同时,矩阵点乘即为哈达玛积(Hadamard Product/Point-wise Product/Element-wise Product/Element-wise Multiplication)
- 哈达玛积其元素定义为两个矩阵对应元素的乘积
- 矩阵各个对应元素相乘, 这个时候要求两个矩阵必须同样大小
A = [ a 11 a 12 a 13 a 21 a 22 a 23 a 31 a 32 a 33 ] 3 × 3 , B = [ b 11 b 12 b 13 b 21 b 22 b 23 b 31 b 32 b 33 ] 3 × 3 A = \left[\begin{matrix} \red{a_{11}} & \red{a_{12}} & \red{a_{13}} \\ a_{21} & a_{22} & a_{23} \\ a_{31} & a_{32} & a_{33} \\ \end{matrix}\right]_{\green{3 \times 3}}, \quad B = \left[\begin{matrix} \blue{b_{11}} & b_{12} & b_{13} \\ \blue{b_{21}} & b_{22} & b_{23} \\ \blue{b_{31}} & b_{32} & b_{33} \\ \end{matrix}\right]_{\green{3 \times 3}} A= a11a21a31a12a22a32a13a23a33 3×3,B= b11b21b31b12b22b32b13b23b33 3×3
C = A 3 × 3 B 3 × 3 = C 3 × 3 = [ a 11 b 11 a 12 b 12 a 13 b 13 a 21 b 21 a 22 b 22 a 23 b 23 a 31 b 31 a 32 b 32 a 33 b 33 ] 3 × 3 \begin{aligned} C &= A_{\green{3 \times 3}} B_{\green{3 \times 3}} = C_{\green{3 \times 3}} \\ &=\left[\begin{matrix} \red{a_{11}} \blue{b_{11}} & \red{a_{12}} b_{12} & \red{a_{13}} b_{13} \\ a_{21} \blue{b_{21}} & a_{22} b_{22} & a_{23} b_{23} \\ a_{31} \blue{b_{31}} & a_{32} b_{32} & a_{33} b_{33} \\ \end{matrix}\right]_{\green{3 \times 3}} \end{aligned} C=A3×3B3×3=C3×3= a11b11a21b21a31b31a12b12a22b22a32b32a13b13a23b23a33b33 3×3
A = [1 2 3;
4 5 6;
7 8 9];
B = [1 2 3;
4 5 6;
7 8 9];
Matlab语法:C = A .* B
>> C = A .* B
C =
1 4 9
16 25 36
49 64 81
>> C = A * B
C =
30 36 42
66 81 96
102 126 150
>> C = dot(A, B)
C =
66 93 126
>> C = cross(A, B)
C =
0 0 0
0 0 0
0 0 0
4. 矩阵基本运算 - 克罗内克积,Kronecker product
A = [ a 11 a 12 a 13 a 21 a 22 a 23 ] 2 × 3 , B = [ b 11 b 12 b 21 b 22 b 31 b 32 ] 3 × 2 A = \left[\begin{matrix} \red{a_{11}} & \red{a_{12}} & \red{a_{13}} \\ a_{21} & a_{22} & a_{23} \\ \end{matrix}\right]_{\green{2 \times 3}}, \quad B = \left[\begin{matrix} \blue{b_{11}} & b_{12} \\ \blue{b_{21}} & b_{22} \\ \blue{b_{31}} & b_{32} \\ \end{matrix}\right]_{\green{3 \times 2}} A=[a11a21a12a22a13a23]2×3,B= b11b21b31b12b22b32 3×2
C = A 2 × 3 B 3 × 2 = C 6 × 6 = [ a 11 b 11 a 11 b 12 a 12 b 11 a 12 b 12 a 13 b 11 a 13 b 12 a 11 b 21 a 11 b 22 a 12 b 21 a 12 b 22 a 13 b 21 a 13 b 22 a 11 b 31 a 11 b 32 a 12 b 31 a 12 b 32 a 13 b 31 a 13 b 32 a 21 b 11 a 21 b 12 a 22 b 11 a 22 b 12 a 23 b 11 a 23 b 12 a 21 b 21 a 21 b 22 a 22 b 21 a 22 b 22 a 23 b 21 a 23 b 22 a 21 b 31 a 21 b 32 a 22 b 31 a 22 b 32 a 23 b 31 a 23 b 32 ] 6 × 6 \begin{aligned} C &= A_{\green{2 \times 3}} B_{\green{3 \times 2}} = C_{\green{6 \times 6}} \\ &=\left[\begin{matrix} \red{a_{11}} \blue{b_{11}} & \red{a_{11}} b_{12} & \red{a_{12}} \blue{b_{11}} & \red{a_{12}} b_{12} & \red{a_{13}} \blue{b_{11}} & \red{a_{13}} b_{12} \\ \red{a_{11}} \blue{b_{21}} & \red{a_{11}} b_{22} & \red{a_{12}} \blue{b_{21}} & \red{a_{12}} b_{22} & \red{a_{13}} \blue{b_{21}} & \red{a_{13}} b_{22} \\ \red{a_{11}} \blue{b_{31}} & \red{a_{11}} b_{32} & \red{a_{12}} \blue{b_{31}} & \red{a_{12}} b_{32} & \red{a_{13}} \blue{b_{31}} & \red{a_{13}} b_{32} \\ a_{21} \blue{b_{11}} & a_{21} b_{12} & a_{22} \blue{b_{11}} & a_{22} b_{12} & a_{23} \blue{b_{11}} & a_{23} b_{12} \\ a_{21} \blue{b_{21}} & a_{21} b_{22} & a_{22} \blue{b_{21}} & a_{22} b_{22} & a_{23} \blue{b_{21}} & a_{23} b_{22} \\ a_{21} \blue{b_{31}} & a_{21} b_{32} & a_{22} \blue{b_{31}} & a_{22} b_{32} & a_{23} \blue{b_{31}} & a_{23} b_{32} \\ \end{matrix}\right]_{\green{6 \times 6}} \end{aligned} C=A2×3B3×2=C6×6= a11b11a11b21a11b31a21b11a21b21a21b31a11b12a11b22a11b32a21b12a21b22a21b32a12b11a12b21a12b31a22b11a22b21a22b31a12b12a12b22a12b32a22b12a22b22a22b32a13b11a13b21a13b31a23b11a23b21a23b31a13b12a13b22a13b32a23b12a23b22a23b32 6×6
A = [1 2 3;
4 5 6];
B = [1 2;
3 4;
5 6];
Matlab语法:C = kron(A, B)
>> C = kron(A, B)
C =
1 2 2 4 3 6
3 4 6 8 9 12
5 6 10 12 15 18
4 8 5 10 6 12
12 16 15 20 18 24
20 24 25 30 30 36
>> C = A * B
C =
22 28
49 64
>> C = dot(A, B)
错误使用 dot
A 和 B 的大小必须相同。
>> C = cross(A, B)
错误使用 cross
A 和 B 的大小必须相同。
5. Matlab矩阵运算 - 普通乘积 *
这个比较简单,不再过多解释。
6. Matlab矩阵运算 - 点乘 .*
A = [ a 11 a 12 a 21 a 22 a 31 a 32 a 41 a 42 ] 4 × 2 , B = [ b 11 b 21 b 31 b 41 ] 4 × 1 A = \left[\begin{matrix} \red{a_{11}} & \red{a_{12}} \\ a_{21} & a_{22} \\ a_{31} & a_{32} \\ a_{41} & a_{42} \\ \end{matrix}\right]_{\green{4 \times 2}}, \quad B = \left[\begin{matrix} \blue{b_{11}} \\ \blue{b_{21}} \\ \blue{b_{31}} \\ \blue{b_{41}} \\ \end{matrix}\right]_{\green{4 \times 1}} A= a11a21a31a41a12a22a32a42 4×2,B= b11b21b31b41 4×1
C = A 4 × 2 ⊙ B 4 × 1 = C 4 × 2 = [ a 11 b 11 a 12 b 11 a 21 b 21 a 22 b 21 a 31 b 31 a 32 b 31 a 41 b 41 a 42 b 41 ] 4 × 2 \begin{aligned} C &= A_{\green{4 \times 2}} \odot B_{\green{4 \times 1}} = C_{\green{4 \times 2}} \\ &=\left[\begin{matrix} \red{a_{11}} \blue{b_{11}} & \red{a_{12}} \blue{b_{11}} \\ a_{21} \blue{b_{21}} & a_{22} \blue{b_{21}} \\ a_{31} \blue{b_{31}} & a_{32} \blue{b_{31}} \\ a_{41} \blue{b_{41}} & a_{42} \blue{b_{41}} \\ \end{matrix}\right]_{\green{4 \times 2}} \end{aligned} C=A4×2⊙B4×1=C4×2= a11b11a21b21a31b31a41b41a12b11a22b21a32b31a42b41 4×2
A = [1 2;
3 4;
5 6;
7 8];
B = [1;
2;
3;
4];
>> C = A .* B
C =
1 2
6 8
15 18
28 32
A = [ a 11 a 21 ] 2 × 1 , B = [ b 11 b 12 b 13 ] 1 × 3 A = \left[\begin{matrix} \red{a_{11}} \\ a_{21} \\ \end{matrix}\right]_{\green{2 \times 1}}, \quad B = \left[\begin{matrix} \blue{b_{11}} & b_{12} & b_{13} \\ \end{matrix}\right]_{\green{1 \times 3}} A=[a11a21]2×1,B=[b11b12b13]1×3
C = A 2 × 1 ⊙ B 1 × 3 = C 2 × 3 = [ a 11 b 11 a 11 b 12 a 11 b 13 a 21 b 11 a 21 b 12 a 21 b 13 ] 2 × 3 \begin{aligned} C &= A_{\green{2 \times 1}} \odot B_{\green{1 \times 3}} = C_{\green{2 \times 3}} \\ &=\left[\begin{matrix} \red{a_{11}} \blue{b_{11}} & \red{a_{11}} \blue{b_{12}} & \red{a_{11}} \blue{b_{13}} \\ a_{21} \blue{b_{11}} & a_{21} b_{12} & a_{21} b_{13} \\ \end{matrix}\right]_{\green{2 \times 3}} \end{aligned} C=A2×1⊙B1×3=C2×3=[a11b11a21b11a11b12a21b12a11b13a21b13]2×3
A = [1;
2];
B = [1 2 3];
>> C = A .* B
C =
1 2 3
2 4 6
A = [ a 11 a 21 ] 2 × 1 , B = [ b 11 b 12 b 13 b 21 b 22 b 23 ] 2 × 3 A = \left[\begin{matrix} \red{a_{11}} \\ a_{21} \\ \end{matrix}\right]_{\green{2 \times 1}}, \quad B = \left[\begin{matrix} \blue{b_{11}} & b_{12} & b_{13} \\ \blue{b_{21}} & b_{22} & b_{23} \\ \end{matrix}\right]_{\green{2 \times 3}} A=[a11a21]2×1,B=[b11b21b12b22b13b23]2×3
C = A 2 × 1 ⊙ B 2 × 3 = C 2 × 3 = [ a 11 b 11 a 11 b 12 a 11 b 13 a 21 b 12 a 21 b 22 a 21 b 23 ] 2 × 3 \begin{aligned} C &= A_{\green{2 \times 1}} \odot B_{\green{2 \times 3}} = C_{\green{2 \times 3}} \\ &=\left[\begin{matrix} \red{a_{11}} \blue{b_{11}} & \red{a_{11}} \blue{b_{12}} & \red{a_{11}} \blue{b_{13}} \\ a_{21} \blue{b_{12}} & a_{21} b_{22} & a_{21} b_{23} \\ \end{matrix}\right]_{\green{2 \times 3}} \end{aligned} C=A2×1⊙B2×3=C2×3=[a11b11a21b12a11b12a21b22a11b13a21b23]2×3
A = [1;
2];
B = [1 2 3;
4 5 6];
>> C = A .* B
C =
1 2 3
8 10 12
7. Matlab矩阵运算 - 点积 dot()
C = dot(A,B)
returns the scalar dot product of A and B.
- If A and B are vectors, then they must have the same length.
- If A and B are matrices or multidimensional arrays, then they must have the same size. In this case, the dot function treats A and B as collections of vectors. The function calculates the dot product of corresponding vectors along the first array dimension whose size does not equal 1.
C = dot(A,B,dim)
evaluates the dot product of A and B along dimension, dim. The dim input is a positive integer scalar.
8. Matlab矩阵运算 - 叉乘 cross()
C = cross(A,B)
returns the cross product of A and B.文章来源:https://www.toymoban.com/news/detail-443452.html
- If A and B are vectors, then they must have a length of 3.
- If A and B are matrices or multidimensional arrays, then they must have the same size. In this case, the cross function treats A and B as collections of three-element vectors. The function calculates the cross product of corresponding vectors along the first array dimension whose size equals 3.
C = cross(A,B,dim)
evaluates the cross product of arrays A and B along dimension, dim. A and B must have the same size, and both size(A,dim) and size(B,dim) must be 3. The dim input is a positive integer scalar.文章来源地址https://www.toymoban.com/news/detail-443452.html
Ref
- 矩阵和向量的点乘与叉乘
- 几种矩阵乘法总结
- 矩阵乘法,还可以这样算?
- 向量和矩阵的点乘和叉乘
到了这里,关于【数理知识】矩阵普通乘积,哈达玛积,克罗内克积,点乘,点积,叉乘,matlab代码实现的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!