MATLAB算術運算
MATLAB允許兩種不同類型的算術運算:
-
矩陣算術運算
-
陣列算術運算
矩陣的算術運算是線性代數中的定義相同。執行數組操作,無論是在一維和多維數組元素的元素。
矩陣運算符和數組運營商是有區彆的句點(.)符號。然而,由於加法和減法運算矩陣和陣列是相同的,操作者這兩種情況下是相同的。
下表給出了運算符的簡要說明:
操作符 | 描述 |
---|---|
+ | Addition or unary plus. A+B adds 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. |
- | 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,
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. |
例子
下麵的例子顯示使用標量數據的算術運算符。創建一個腳本文件,用下麵的代碼:
a = 10; b = 20; c = a + b d = a - b e = a * b f = a / b g = a b x = 7; y = 3; z = x ^ y
當運行該文件,它會產生以下結果:
c = 30 d = -10 e = 200 f = 0.5000 g = 2 z = 343
算術運算功能
除了在上述的算術運算符,MATLAB 用於類似的目的提供了以下的命令/功能:
函數 | 描述 |
---|---|
uplus(a) | Unary plus; increments by the amount a |
plus (a,b) | Plus; returns a + b |
uminus(a) | Unary minus; decrements by the amount a |
minus(a, b) | Minus; returns a - b |
times(a, b) | Array multiply; returns a.*b |
mtimes(a, b) | Matrix multiplication; returns a* b |
rdivide(a, b) | Right array division; returns a ./ b |
ldivide(a, b) | Left array division; returns a. b |
mrdivide(A, B) | Solve systems of linear equations xA = B for x |
mldivide(A, B) | Solve systems of linear equations Ax = B for x |
power(a, b) | Array power; returns a.^b |
mpower(a, b) | Matrix power; returns a ^ b |
cumprod(A) |
Cumulative product; returns an array the same size as the array A containing the cumulative product.
|
cumprod(A, dim) | Returns the cumulative product along dimension dim. |
cumsum(A) |
Cumulative sum; returns an array A containing the cumulative sum.
|
cumsum(A, dim) | returns the cumulative sum of the elements along dimension dim. |
diff(X) |
Differences and approximate derivatives; calculates differences between adjacent elements of X.
|
diff(X,n) | Applies diff recursively n times, resulting in the nth difference. |
diff(X,n,dim) | It is the nth difference function calculated along the dimension specified by scalar dim. If order n equals or exceeds the length of dimension dim, diff returns an empty array. |
prod(A) |
Product of array elements; returns the product of the array elements of A.
|
prod(A,dim) | Returns the products along dimension dim. For example, if A is a matrix, prod(A,2) is a column vector containing the products of each row. |
prod(___,datatype) | multiplies in and returns an array in the class specified by datatype. |
sum(A) |
|
sum(A,dim) | Sums along the dimension of A specified by scalar dim. |
sum(..., 'double')
sum(..., dim,'double') |
Perform additions in double-precision and return an answer of type double, even if A has data type single or an integer data type. This is the default for integer data types. |
sum(..., 'native')
sum(..., dim,'native') |
Perform additions in the native data type of A and return an answer of the same data type. This is the default for single and double. |
ceil(A) | Round toward positive infinity; rounds the elements of A to the nearest integers greater than or equal to A. |
fix(A) | Round toward zero |
floor(A) | Round toward negative infinity; rounds the elements of A to the nearest integers less than or equal to A. |
idivide(a, b)
idivide(a, b,'fix') |
Integer division with rounding option; is the same as a./b except that fractional quotients are rounded toward zero to the nearest integers. |
idivide(a, b, 'round') | Fractional quotients are rounded to the nearest integers. |
idivide(A, B, 'floor') | Fractional quotients are rounded toward negative infinity to the nearest integers. |
idivide(A, B, 'ceil') | Fractional quotients are rounded toward infinity to the nearest integers. |
mod (X,Y) |
Modulus after division; returns X - n.*Y where n = floor(X./Y). If Y is not an integer and the quotient X./Y is within roundoff error of an integer, then n is that integer. The inputs X and Y must be real arrays of the same size, or real scalars (provided Y ~=0).
Please note:
|
rem (X,Y) |
Remainder after division; returns X - n.*Y where n = fix(X./Y). If Y is not an integer and the quotient X./Y is within roundoff error of an integer, then n is that integer. The inputs X and Y must be real arrays of the same size, or real scalars(provided Y ~=0).
Please note that:
|
round(X) | Round to nearest integer; rounds the elements of X to the nearest integers. Positive elements with a fractional part of 0.5 round up to the nearest positive integer. Negative elements with a fractional part of -0.5 round down to the nearest negative integer. |