位置:首頁 > 高級語言 > Matlab教學 > MATLAB運算符

MATLAB運算符

運算符是一個符號,它告訴編譯器執行特定的數學或邏輯操作。 MATLAB 設計工作主要是對整個矩陣和陣列。因此,運算符在 MATLAB 工作標和非標量數據。 MATLAB 允許以下類型的基本運算:

  • 算術運算符

  • 關係運算符

  • 邏輯運算符

  • 位運算

  • 集合運算

算術運算符

MATLAB允許兩種不同類型的算術運算:

  • 矩陣算術運算

  • 陣列算術運算

矩陣的算術運算是線性代數中的定義相同。執行數組操作,無論是在一維和多維數組元素的元素。

矩陣運算符和數組運算符是有區彆的句號(.)符號。然而,由於加法和減法運算矩陣和陣列是相同的,操作者這兩種情況下是相同的。下表給出了運算符的簡要說明:

詳細例子

運算符 描述
+ 加法或一元加號。A + B將A和B。 A和B必須具有相同的尺寸,除非一個人是一個標量。一個標量,可以被添加到任何大小的矩陣。
- Subtraction or unary minus. A-B subtracts 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.
* Matrix multiplication. C = A*B is the linear algebraic product of the matrices A and B. More precisely,

Matrix Multiplication

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

.* 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.
/ Slash or matrix right division. B/A is roughly the same as B*inv(A). More precisely, B/A = (A'B')'.
./ 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.
Backslash or matrix left division. If A is a square matrix, AB 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 = AB is the solution to the equation AX = B. A warning message is displayed if A is badly scaled or nearly singular.
. 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.
^ 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.
.^ 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.
' Matrix transpose. A' is the linear algebraic transpose of A. For complex matrices, this is the complex conjugate transpose.
.' Array transpose. A.' is the array transpose of A. For complex matrices, this does not involve conjugation.

關係運算符

關係運算符標和非標量數據上也能正常工作。關係運算符對數組進行元素元素元素設置為邏輯1(真)的關係是真實的和元素設置為邏輯0(假),它是兩個陣列,並返回一個同樣大小的邏輯陣列之間的比較。

下表顯示了 MATLAB 中的關係運算符:

Show Examples

運算符 描述
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
== Equal to
~= Not equal to

邏輯運算符

MATLAB提供了兩種類型的邏輯運算符和函數:

  • Element-wise -這些運算符的邏輯陣列上運行相應的元素。

  • Short-circuit -這些運算上的標量,邏輯表達式。

Element-wise 的邏輯運算符操作元素元素邏輯陣列。符號&,|和〜邏輯數組運算符AND,OR,NOT。

允許短路短路邏輯運算符,邏輯運算。符號 && 和 | | 是短路邏輯符 AND 和 OR。

Show Examples

位運算

位運算符位和執行位位操作。 &,|和^的真值表如下:

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提供位運算,如'位','位'和'位不操作,移位操作等各種函數

以下的表格顯示了常用的按位運算:

Show Examples

函數 目的/作用
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提供各種功能集合運算,如集,交集和測試組成員等。

下表顯示了一些常用的設置操作:

Show Examples

函數 描述
intersect(A,B) Set intersection of two arrays; returns the values common to both A and B. The values returned are in sorted order.
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.
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).
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).
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.
issorted(A, 'rows') Returns logical 1 (true) if the rows of two-dimensional matrix A are 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.
setdiff(A,B) Set difference of two arrays; returns the values in A that are not in B. The values in the returned array are in sorted order.
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.

setxor Set exclusive OR of two arrays
union Set union of two arrays
unique Unique values in array