English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
MATLAB allows two different types of arithmetic operations-
Matrix arithmetic operations
Array arithmetic operations
Matrix arithmetic operations are the same as those defined in linear algebra. Array operations are performed on each element of one-dimensional and multi-dimensional arrays individually.
The matrix operator and array operator are distinguished by the dot (.) symbol. However, since the addition and subtraction operations of matrices and arrays are the same, the operators in both cases are the same.
The following table briefly introduces the operators-
Function | Operator and Description |
---|---|
1 | + Addition or unary addition. A + B adds the values stored in variables A and B. A and B must be the same size unless one of them is a scalar. Scalars can be added to matrices of any size. |
2 | - Subtraction or unary subtraction. AB subtracts the value of B from A. A and B must be the same size unless one of them is a scalar. Scalars can be subtracted from matrices of any size. |
3 | * Matrix multiplication. C = A * B is the linear algebraic product of matrices A and B. More precisely, For non-scalar A and B, the number of columns of A must equal the number of rows of B. Scalars can be multiplied by matrices of any size. |
4 | .* Array multiplication. A. * B is the element-wise product of 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 and B *approximately the same as inv(A). More accurately, B / A = (A'\ B')'. |
6 | ./ Array right division. A./B is the element A(i,j)/of the matrix B(i,j). A and B must be the same size, unless one of them is a scalar. |
7 | \ Backslash or matrix left division. If A is a square matrix, then A \ B is equivalent to inv(A)* B is approximately the same, but calculated differently. If A is an n×n matrix and B is a column vector with n components, or a matrix with several such columns, then X = A \ B is the equationAX = Bsolution. A warning message is displayed if A is scaled incorrectly or nearly singular. |
8 | .\ Array left division. A. \ B is the element B(i,j)/of the matrix A(i,j). A and B must be the same size, unless one of them is a scalar. |
9 | ^ Matrix power. If p is a scalar, then X ^ p is X raised to the power p. If p is an integer, the power is calculated by repeated squaring. If the integer is negative, X is first inverted. For other values of p, the calculation involves eigenvalues and eigenvectors, so 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) raised to the power B(i,j). A and B must be 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 conjugate transpose. |
12 | .' Array transpose. One. ' is the transpose of A. For complex matrices, this does not involve conjugation. |
The following example shows the use of arithmetic operators for scalar data. Create a script file using the following code-
a = 10; b = 20; c = a + right array division; returns a. d = a - right array division; returns a. e = a * right array division; returns a. f = a / right array division; returns a. g = a \ b x = 7; y = 3; z = x ^ yWhen the file is executed, it will produce the following results-
c = 30 c = -10 d = 2e = 005f = 0. 0000 2 g = 343
Arithmetic operation functions/In addition to the above arithmetic operators, MATLAB also provides the following commands for similar purposes-
Function | Serial number |
---|---|
1 | Function description uplus(a) |
2 | unary plus; increment by the number of a plus (a,b) + right array division; returns a. |
3 | add; returns a uminus(a) |
4 | unary minus; decrease by amount a minus(a, b)-right array division; returns a. |
5 | subtract; returns a times(a, b)* right array division; returns a. |
6 | array multiplication; returns a. mtimes(a, b) * right array division; returns a. |
7 | matrix multiplication; returns a rdivide(a, b)/ right array division; returns a. |
8 | b ldivide(a, b) |
9 | left array division; returns a.\ b mldivide(A, B)mrdivide(A, B)XA = BX |
10 | for mldivide(A, B)solving the system of linear equations Ax = bused forX |
11 | power(a, b) array power; returns a.^ b |
12 | mpower(a, b) matrix power; returns a ^ b |
13 | cumprod(A) Cumulative product; returns an array of the same size as the array containing the cumulative product.
|
14 | cumprod(A, dim) Returns alongdimof the cumulative product. |
15 | cumsum(A) Cumulative sum returns an array A containing the cumulative sum.
|
16 | cumsum(A, dim) Returns along the dimensiondimof the cumulative sum of its elements. |
17 | diff(X) difference and approximate derivative; calculate the difference between adjacent elements of X.
|
18 | diff(X,n) applied recursivelydiff n times, resulting in the nth difference. |
19 | diff(X,n,dim) It is the nth difference function calculated along the dimension specified by the scalar dark angle. If the order n is equal to or greater than the length of the dimension dim, then diff returns an empty array. |
20 | prod(A) Product of array elements; returns the product of the elements of the array A.
If the input A is a single number, the prod function calculates and returns B as a single number. For all other numeric and logical data types, prod calculates B and returns it as double. |
21 | prod(A, dim) Returns the product with dimension dim. For example, if A is a matrix, then prod(A,2) is a column vector containing the product of each row. |
22 | prod(___, datatype) Multiply and return an array in the class specified by the data type. |
23 | sum(A)
|
24 | sum(A, dim) Along the scalardimSpecifiedASum along the dimension. |
25 | sum(..., 'double') sum(..., dim, 'double') Perform addition in double precision even if A has a single data type or integer data type, and return a double precision type answer. This is the default setting for integer data types. |
26 | sum(..., 'native') sum(..., dim, 'native') Perform addition with the native data type of A and return an answer of the same data type. This is the default setting for single and double. |
27 | ceil(A) Round to positive infinity; round the elements of A to the nearest integer greater than or equal to A. |
28 | fix(A) Round to zero |
29 | floor(A) Round to negative infinity; round the elements of A to the nearest integer less than or equal to A. |
30 | idivide(a, b) idivide(a, b, 'fix') Integer division with rounding options; similar to a./b is the same, except that the fractional quotient is rounded to the nearest integer towards zero. |
31 | idivide(a, b, 'round') the fractional quotient is rounded to the nearest integer. |
32 | idivide(A, B, 'floor') the fractional quotient is rounded to the nearest integer towards negative infinity. |
33 | idivide(A, B, 'ceil') the fractional quotient is rounded to the nearest integer towards infinity. |
34 | mod (X,Y) the modulus after division; return X-n.* Y, where n = floor(X./Y). If Y is not an integer and the quotient X./Y is within the rounding error of an integer, then n is that integer. The input X and Y must be real arrays or real scalars of the same size (Y ~ 0 provided). Note-
|
35 | rem (X,Y) the remainder after division; return X-n.* Y, where n = fix(X./Y). If Y is not an integer and the quotient X./Y is within the rounding error of an integer, then n is that integer. The input X and Y must be real arrays or real scalars of the same size (Y ~ 0 provided). Note-
|
36 | round(X) rounded to the nearest integer; round the elements of X to the nearest integer. The decimal part is 0.5positive elements will be rounded to the nearest positive integer. The decimal part is-0.5negative elements will be rounded to the nearest negative integer. |