Documentation

Array vs. Matrix Operations

介绍

MATLAB®has two different types of arithmetic operations: array operations and matrix operations. You can use these arithmetic operations to perform numeric computations, for example, adding two numbers, raising the elements of an array to a given power, or multiplying two matrices.

Matrix operations follow the rules of linear algebra. By contrast, array operations execute element by element operations and support multidimensional arrays. The period character ()将数组操作与矩阵操作区分开。但是,由于矩阵和阵列操作对于加法和减法是相同的,因此字符对。+.-是不必要的。

Array Operations

数组操作通过元素操作执行元素,对向量,矩阵和多维数组的相应元素进行执行。如果操作数具有相同的大小,则第一操作数中的每个元素都与第二操作数中同一位置中的元素匹配。如果操作数具有兼容尺寸,则根据需要将每个输入隐式扩展以匹配另一个输入。有关更多信息,请参阅兼容阵列大小用于基本操作

As a simple example, you can add two vectors with the same size.

A = [1 1 1]
A = 1 1 1
B = [1 2 3]
B = 1 2 3
a+b
ANS = 2 3 4

如果一个操作数是标量,而另一个则不是标量,则MATLAB隐式将标量扩展到与其他操作数相同的大小。例如,您可以计算标量和矩阵的元素产品。

a = [1 2 3;1 2 3]
A = 1 2 3 1 2 3
3.*A
ans = 3 6 9 3 6 9

Implicit expansion also works if you subtract a 1-by-3 vector from a 3-by-3 matrix because the two sizes are compatible. When you perform the subtraction, the vector is implicitly expanded to become a 3-by-3 matrix.

A = [1 1 1; 2 2 2; 3 3 3]
A = 1 1 1 2 2 2 3 3 3
m = [2 4 6]
M = 2 4 6
ans = -1 -3 -5 0 -2 -4 1 -1 -3

A row vector and a column vector have compatible sizes. If you add a 1-by-3 vector to a 2-by-1 vector, then each vector implicitly expands into a 2-by-3 matrix before MATLAB executes the element-wise addition.

x = [1 2 3]
x = 1 2 3
y = [10;15]
y = 10 15
x + y
ans = 11 12 13 16 17 18

如果两个操作数的尺寸不兼容,则会出现错误。

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
矩阵维度必须同意。

下表提供了MATLAB中算术阵列运算符的摘要。有关特定于功能的信息,请单击上一列中“功能参考”页面的链接。

Operator

Purpose

Description

参考页

+

添加

a+b添加AB

+

Unary plus

+areturnsA

uplus

-

减法

A-BsubtractsBA

minus

-

Unary minus

-一个negates the elements ofA

uminus

。*

Element-wise multiplication

A.*B中的元素的产品吗AB

时代

。^

元素能力

A.^B是带有元素的矩阵A(i,j)B(i,j)力量。

力量
./

Right array division

A./B是带有元素的矩阵A(i,j)/B(i,j)

rdivide

。\

Left array division

A.\B是带有元素的矩阵b(i,j)/a(i,j)

ldivide

。'

Array transpose

一个。'is the array transpose ofA。对于复杂的矩阵,这不涉及共轭。

转置

矩阵操作

Matrix operations follow the rules of linear algebra and are not compatible with multidimensional arrays. The required size and shape of the inputs in relation to one another depends on the operation. For nonscalar inputs, the matrix operators generally calculate different answers than their array operator counterparts.

For example, if you use the matrix right division operator,/,要分配两个矩阵,矩阵必须具有相同数量的列。但是,如果使用矩阵乘法运算符,*, to multiply two matrices, then the matrices must have a common内维。That is, the number of columns in the first input must be equal to the number of rows in the second input. The matrix multiplication operator calculates the product of two matrices with the formula,

C ( i , j ) = k = 1 n A ( i , k ) B ( k , j )

To see this, you can calculate the product of two matrices.

A = [1 3;2 4]
A = 1 3 2 4
b = [3 0; 1 5]
B = 3 0 1 5
A*b
ANS = 6 15 10 20

The previous matrix product is not equal to the following element-wise product.

A.*B
ans = 3 0 2 20

The following table provides a summary of matrix arithmetic operators in MATLAB. For function-specific information, click the link to the function reference page in the last column.

Operator

Purpose

Description

参考页

*

Matrix multiplication

C =A*bis the linear algebraic product of the matricesAB。列数Amust equal the number of rows ofB

mtimes

\

矩阵左师

x = A\B是方程式的解决方案Ax=B。MatricesABmust have the same number of rows.

mldivide

/

Matrix right division

x = B/A是方程式的解决方案xA=B。MatricesABmust have the same number of columns. In terms of the left division operator,B/A = (A'\B')'

mrdivide

^

Matrix power

a^bisA到力量B, 如果Bis a scalar. For other values ofB, the calculation involves eigenvalues and eigenvectors.

mpower

'

复杂的共轭转置

A'是线性代数转置的A。For complex matrices, this is the complex conjugate transpose.

ctranspose

相关话题