Wednesday 22 December 2010

Arithmetic Operators in MATLAB

Arithmetic Operators + - * / \ ^ '

Matrix and array arithmetic

Syntax

A+B
A-B
A*B
A.*B
A/B
A./B
A\B
A.\B
A^B
A.^B
A'
A.'

Description

MATLAB® software has two different types of arithmetic operations. Matrix arithmetic operations are defined by the rules of linear algebra. Array arithmetic operations are carried out element by element, and can be used with multidimensional arrays. The period character (.) 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 not used.
+ 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')'. See the reference page for mrdivide for more information.
./ 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, 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. See the reference page for mldivide for more information.
If A is an m-by-n matrix with m ~= n and B is a column vector with m components, or a matrix with several such columns, then X = A\B is the solution in the least squares sense to the under- or overdetermined system of equations AX = B. The effective rank, k, of A is determined from the QR decomposition with pivoting (see Algorithm for details). A solution X is computed that has at most k nonzero components per column. If k < n, this is usually not the same solution as pinv(A)*B, which is the least squares solution with the smallest norm .
.\ 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.
If x is a scalar and P is a matrix, x^P is x raised to the matrix power P using eigenvalues and eigenvectors. X^P, where X and P are both matrices, is an error.
.^ 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.

Nondouble Data Type Support

This section describes the arithmetic operators' support for data types other than double.

Data Type single

You can apply any of the arithmetic operators to arrays of type single and MATLAB software returns an answer of type single. You can also combine an array of type double with an array of type single, and the result has type single.

Integer Data Types

You can apply most of the arithmetic operators to real arrays of the following integer data types:
  • int8 and uint8
  • int16 and uint16
  • int32 and uint32
All operands must have the same integer data type and MATLAB returns an answer of that type.
    Note   The arithmetic operators do not support operations on the data types int64 or uint64. Except for the unary operators +A and A.', the arithmetic operators do not support operations on complex arrays of any integer data type.
For example,
x = int8(3) + int8(4);
class(x)

ans =

int8
The following table lists the binary arithmetic operators that you can apply to arrays of the same integer data type. In the table, A and B are arrays of the same integer data type and c is a scalar of type double or the same type as A and B.
OperationSupport when A and B Have Same Integer Type
+A, -AYes
A+B, A+c, c+BYes
A-B, A-c, c-BYes
A.*BYes
A*c, c*BYes
A*BNo
A/c, c/BYes
A.\B, A./BYes
A\B, A/BNo
A.^BYes, if B has nonnegative integer values.
c^kYes, for a scalar c and a nonnegative scalar integer k, which have the same integer data type or one of which has type double
A.', A'Yes

Combining Integer Data Types with Type Double

For the operations that support integer data types, you can combine a scalar or array of an integer data type with a scalar, but not an array, of type double and the result has the same integer data type as the input of integer type. For example,
y = 5 + int32(7);
class(y)

ans =

int32
However, you cannot combine an array of an integer data type with either of the following:
  • A scalar or array of a different integer data type
  • A scalar or array of type single
The section Numeric Classes, under Classes (Data Types) in the MATLAB Programming Fundamentals documentation, provides more information about operations on nondouble data types.

Remarks

The arithmetic operators have M-file function equivalents, as shown:
Binary additionA+Bplus(A,B)
Unary plus+Auplus(A)
Binary subtractionA-Bminus(A,B)
Unary minus-Auminus(A)
Matrix multiplicationA*Bmtimes(A,B)
Arraywise multiplicationA.*Btimes(A,B)
Matrix right divisionA/Bmrdivide(A,B)
Arraywise right divisionA./Brdivide(A,B)
Matrix left divisionA\Bmldivide(A,B)
Arraywise left divisionA.\Bldivide(A,B)
Matrix powerA^Bmpower(A,B)
Arraywise powerA.^Bpower(A,B)
Complex transposeA'ctranspose(A)
Matrix transposeA.'transpose(A)
    Note   For some toolboxes, the arithmetic operators are overloaded, that is, they perform differently in the context of that toolbox. To see the toolboxes that overload a given operator, type help followed by the operator name. For example, type help plus. The toolboxes that overload plus (+) are listed. For information about using the operator in that toolbox, see the documentation for the toolbox.

Examples

Here are two vectors, and the results of various matrix and array operations on them, printed with format rat.
Matrix OperationsArray Operations
x 1
2
3
y 4
5
6
x' 1 2 3y' 4 5 6
x+y 5
7
9
x-y -3
-3
-3
x + 2 3
4
5
x-2 -1
0
1
x * y Errorx.*y 4
10
18
x'*y 32x'.*y Error
x*y' 4 5 6
8 10 12
12 15 18
x.*y' Error
x*2 2
4
6
x.*2 2
4
6
x\y 16/7x.\y 4
5/2
2
2\x1/2
1
3/2
2./x 2
1
2/3
x/y0 0 1/6
0 0 1/3
0 0 1/2
x./y1/4
2/5
1/2
x/21/2
1
3/2
x./21/2
1
3/2
x^yErrorx.^y 1
32
729
x^2Errorx.^2 1
4
9
2^xError2.^x 2
4
8
(x+i*y)'1 - 4i 2 - 5i 3 - 6i  
(x+i*y).'1 + 4i 2 + 5i 3 + 6i  

Diagnostics

  • From matrix division, if a square A is singular,
    Warning: Matrix is singular to working precision.
  • From elementwise division, if the divisor has zero elements,
    Warning: Divide by zero.
    Matrix division and elementwise division can produce NaNs or Infs where appropriate.
  • If the inverse was found, but is not reliable,
    Warning: Matrix is close to singular or badly scaled.
        Results may be inaccurate.  RCOND = xxx
  • From matrix division, if a nonsquare A is rank deficient,
    Warning: Rank deficient, rank = xxx tol = xxx

See Also

mldivide, mrdivide, chol, det, inv, lu, orth, permute, ipermute, qr, rref

No comments:

Post a Comment