阵列与矩阵操作
Introduction
MATLAB®有两种不同类型的算术操作:数组操作和矩阵操作。您可以使用这些算术操作执行数字计算,例如,添加两个数字,将数组的元素提高到给定的功率,或乘以两个矩阵。
矩阵操作遵循线性代数的规则。相比之下,数组操作通过元素操作执行元素,并支持多维数组。金宝app周期字符(。
)distinguishes the array operations from the matrix operations. However, since the matrix and array operations are the same for addition and subtraction, the character pairs。+
and。-
are unnecessary.
数组操作
一种rray operations execute element by element operations on corresponding elements of vectors, matrices, and multidimensional arrays. If the operands have the same size, then each element in the first operand gets matched up with the element in the same location in the second operand. If the operands have compatible sizes, then each input is implicitly expanded as needed to match the size of the other. For more information, seeCompatible Array Sizes for Basic Operations。
简单地说,您可以添加两个具有相同大小的向量。
a = [1 1 1]
a = 1 1 1
b = [1 2 3]
b=12 3
一种+b
ans = 2 3 4
If one operand is a scalar and the other is not, then MATLAB implicitly expands the scalar to be the same size as the other operand. For example, you can compute the element-wise product of a scalar and a matrix.
一种=[1 2 3; 1 2 3]
a = 1 2 3 1 2 3
3.*a
ANS = 3 6 9 3 6 9
如果您从3 x-3矩阵中减去1 by-3向量,则隐式扩展也有效,因为两种尺寸是兼容的。当您执行减法时,向量会隐式扩展为3 x-3矩阵。
a = [1 1 1;2 2 2;3 3 3]
a = 1 1 1 2 2 2 3 3 3 3 3
m = [2 4 6]
m = 2 4 6
一种-m
ANS = -1 -3 -5 0 -2 -4 1 -1 -1 -3
行矢量和列向量具有兼容大小。如果将1 x-3向量添加到2 x-1向量,则每个向量都隐含地扩展到2 x-3矩阵,然后MATLAB执行元素添加。
x = [1 2 3]
x = 1 2 3
y = [10; 15]
y = 10 15
x + y
ANS = 11 12 13 16 17 18
If the sizes of the two operands are incompatible, then you get an error.
a = [8 1 6;3 5 7;4 9 2]
a = 8 1 6 3 5 7 4 9 2
m = [2 4]
m = 2 4
一种-m
Matrix dimensions must agree.
The following table provides a summary of arithmetic array operators in MATLAB. For function-specific information, click the link to the function reference page in the last column.
操作员 |
目的 |
描述 |
Reference Page |
---|---|---|---|
|
一种ddition |
|
plus |
|
Unary Plus |
|
Uplus |
|
Subtraction |
|
减 |
|
一元减 |
|
Uminus |
|
元素乘法 |
|
times |
|
Element-wise power |
|
power |
。/ |
右数组部 |
|
rdivide |
|
左阵列部门 |
|
ldivide |
|
阵列转置 |
|
transpose |
Matrix Operations
矩阵操作遵循线性代数的规则,与多维阵列不兼容。相对于彼此相关的输入所需的大小和形状取决于操作。对于非量表输入,矩阵运算符通常计算出与数组操作员对应物不同的答案。
例如,如果您使用矩阵正确的部门操作员,/
,,,,to divide two matrices, the matrices must have the same number of columns. But if you use the matrix multiplication operator,*
,要乘以两个矩阵,那么矩阵必须具有一个共同的一世nner dimension。也就是说,第一个输入中的列数必须等于第二个输入中的行数。矩阵乘法运算符计算两个矩阵的乘积与公式,
要看到这一点,您可以计算两个矩阵的乘积。
a = [1 3; 2 4]
a = 1 3 2 4
b=[3 0;1 5]
b=3 0 1 5
一种*b
ans = 6 15 10 20
先前的矩阵产品不等于以下元素的产品。
A.*b
ans = 3 0 2 20
下表提供了MATLAB中矩阵算术运算符的摘要。有关特定于功能的信息,请单击上一列中“功能参考”页面的链接。
操作员 |
目的 |
描述 |
Reference Page |
---|---|---|---|
|
矩阵乘法 |
|
mtimes |
|
Matrix left division |
|
Mldivide |
|
矩阵右部门 |
|
mrdivide |
|
矩阵功率 |
|
马力 |
|
Complex conjugate transpose |
|
CTRANSPOSE |