📜  MATLAB-运算符

📅  最后修改于: 2020-11-03 09:46:20             🧑  作者: Mango


运算符是一个符号,告诉编译器执行特定的数学或逻辑操作。 MATLAB设计为主要在整个矩阵和数组上运行。因此,MATLAB中的运算符同时处理标量和非标量数据。 MATLAB允许以下类型的基本运算-

  • 算术运算符
  • 关系运算符
  • 逻辑运算符
  • 按位运算
  • 设定作业

算术运算符

MATLAB允许两种不同类型的算术运算-

  • 矩阵算术运算
  • 数组算术运算

矩阵算术运算与线性代数中定义的相同。数组操作在一维和多维数组上逐个元素地执行。

矩阵运算符和数组运算符由句点(。)符号区分。但是,由于矩阵和数组的加法和减法运算相同,因此两种情况的运算符都相同。下表简要介绍了运算符-

显示范例

Sr.No. Operator & Description
1

+

Addition or unary plus. A+B adds the values stored in variables A and B. A and B must have the same size, unless one is a scalar. A scalar can be added to a matrix of any size.

2

Subtraction or unary minus. A-B subtracts the value of B from A. A and B must have the same size, unless one is a scalar. A scalar can be subtracted from a matrix of any size.

3

*

Matrix multiplication. C = A*B is the linear algebraic product of the matrices A and B. More precisely,

Matrix Multiplication

For non-scalar A and B, the number of columns of A must be equal to the number of rows of B. A scalar can multiply a matrix of any size.

4

.*

Array multiplication. A.*B is the element-by-element product of the arrays A and B. A and B must have the same size, unless one of them is a scalar.

5

/

Slash or matrix right division. B/A is roughly the same as B*inv(A). More precisely, B/A = (A’\B’)’.

6

./

Array right division. A./B is the matrix with elements A(i,j)/B(i,j). A and B must have the same size, unless one of them is a scalar.

7

\

Backslash or matrix left division. If A is a square matrix, A\B is roughly the same as inv(A)*B, except it is computed in a different way. If A is an n-by-n matrix and B is a column vector with n components, or a matrix with several such columns, then X = A\B is the solution to the equation AX = B. A warning message is displayed if A is badly scaled or nearly singular.

8

.\

Array left division. A.\B is the matrix with elements B(i,j)/A(i,j). A and B must have the same size, unless one of them is a scalar.

9

^

Matrix power. X^p is X to the power p, if p is a scalar. If p is an integer, the power is computed by repeated squaring. If the integer is negative, X is inverted first. For other values of p, the calculation involves eigenvalues and eigenvectors, such that if [V,D] = eig(X), then X^p = V*D.^p/V.

10

.^

Array power. A.^B is the matrix with elements A(i,j) to the B(i,j) power. A and B must have the same size, unless one of them is a scalar.

11

Matrix transpose. A’ is the linear algebraic transpose of A. For complex matrices, this is the complex conjugate transpose.

12

.’

Array transpose. A.’ is the array transpose of A. For complex matrices, this does not involve conjugation.

关系运算符

关系运算符还可以处理标量和非标量数据。数组的关系运算符在两个数组之间执行逐元素比较,并返回相同大小的逻辑数组,其中元素设置为逻辑1(true),关系为true,元素设置为逻辑0(false)。不。

下表显示了MATLAB中可用的关系运算符-

显示范例

Sr.No. Operator & Description
1

<

Less than

2

<=

Less than or equal to

3

>

Greater than

4

>=

Greater than or equal to

5

==

Equal to

6

~=

Not equal to

逻辑运算符

MATLAB提供两种类型的逻辑运算符和函数-

  • 元素级-这些运算符对逻辑数组的相应元素进行操作。

  • 短路-这些运算符对标量和逻辑表达式进行运算。

逐个元素的逻辑运算符在逻辑数组上逐个元素地进行操作。符号&,|和〜是逻辑数组运算符AND,OR和NOT。

短路逻辑运算符允许逻辑运算短路。符号&&和||是逻辑短路运算符AND和OR。

显示范例

按位运算

按位运算符对位进行运算并执行逐位操作。 &,|和^的真值表如下-

p q p & q p | q p ^ q
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1

假设A = 60;和B = 13;现在以二进制格式,它们将如下所示-

A = 0011 1100

B = 0000 1101

—————–

A&B = 0000 1100

A | B = 0011 1101

A ^ B = 0011 0001

〜A = 1100 0011

MATLAB为按位运算提供了各种功能,例如“按位与”,“按位或”和“按位非”运算,移位运算等。

下表显示了常用的按位运算-

显示范例

Function Purpose
bitand(a, b) Bit-wise AND of integers a and b
bitcmp(a) Bit-wise complement of a
bitget(a,pos) Get bit at specified position pos, in the integer array a
bitor(a, b) Bit-wise OR of integers a and b
bitset(a, pos) Set bit at specific location pos of a
bitshift(a, k) Returns a shifted to the left by k bits, equivalent to multiplying by 2k. Negative values of k correspond to shifting bits right or dividing by 2|k| and rounding to the nearest integer towards negative infinite. Any overflow bits are truncated.
bitxor(a, b) Bit-wise XOR of integers a and b
swapbytes Swap byte ordering

设定作业

MATLAB提供了用于集合操作的各种功能,例如联合,交集和集合成员资格的测试等。

下表显示了一些常用的设置操作-

显示范例

Sr.No. Function & Description
1

intersect(A,B)

Set intersection of two arrays; returns the values common to both A and B. The values returned are in sorted order.

2

intersect(A,B,’rows’)

Treats each row of A and each row of B as single entities and returns the rows common to both A and B. The rows of the returned matrix are in sorted order.

3

ismember(A,B)

Returns an array the same size as A, containing 1 (true) where the elements of A are found in B. Elsewhere, it returns 0 (false).

4

ismember(A,B,’rows’)

Treats each row of A and each row of B as single entities and returns a vector containing 1 (true) where the rows of matrix A are also rows of B. Elsewhere, it returns 0 (false).

5

issorted(A)

Returns logical 1 (true) if the elements of A are in sorted order and logical 0 (false) otherwise. Input A can be a vector or an N-by-1 or 1-by-N cell array of strings. A is considered to be sorted if A and the output of sort(A) are equal.

6

issorted(A, ‘rows’)

Returns logical 1 (true) if the rows of two-dimensional matrix A is in sorted order, and logical 0 (false) otherwise. Matrix A is considered to be sorted if A and the output of sortrows(A) are equal.

7

setdiff(A,B)

Sets difference of two arrays; returns the values in A that are not in B. The values in the returned array are in sorted order.

8

setdiff(A,B,’rows’)

Treats each row of A and each row of B as single entities and returns the rows from A that are not in B. The rows of the returned matrix are in sorted order.

The ‘rows’ option does not support cell arrays.

9

setxor

Sets exclusive OR of two arrays

10

union

Sets union of two arrays

11

unique

Unique values in array