Main Content

阵列与矩阵操作

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

一种+badds一种andb

plus

+

Unary Plus

+一种返回一种

Uplus

-

Subtraction

一种-b减去bfrom一种

-

一元减

-一种否定元素一种

Uminus

。*

元素乘法

A.*b是逐元元素的产品一种andb

times

。^

Element-wise power

A.^b是the matrix with elementsA(i,j)to theB (i, j)力量。

power
。/

右数组部

A./B是the matrix with elements(i, j) / B (i, j)

rdivide

。\ \

左阵列部门

A. \ b是the matrix with elementsB (i, j)/一种((一世,,,,j)

ldivide

。'

阵列转置

一种。'是阵列的转置一种。For complex matrices, this does not involve conjugation.

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。也就是说,第一个输入中的列数必须等于第二个输入中的行数。矩阵乘法运算符计算两个矩阵的乘积与公式,

C (( 一世 ,,,, j = k = 1 n 一种 (( 一世 ,,,, k b (( k ,,,, j

要看到这一点,您可以计算两个矩阵的乘积。

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

*

矩阵乘法

C=一种*b是矩阵的线性代数产物一种andb。The number of columns of一种必须等于行的数量b

mtimes

\ \

Matrix left division

x = a \ b是the solution to the equation斧头=b。矩阵一种andb必须具有相同数量的行。

Mldivide

/

矩阵右部门

x = b/a是the solution to the equationxa=b。矩阵一种andb必须具有相同数量的列。根据左派操作员b/a =(a'\ b')'

mrdivide

^

矩阵功率

一种^b一种力量b,,,,一世fb是标量。对于其他值b,计算涉及特征值和特征向量。

马力

'

Complex conjugate transpose

一种'是the linear algebraic transpose of一种。对于复杂的矩阵,这是复杂的共轭转置。

CTRANSPOSE

Related Topics