|On this page…|
MATLAB® has two different types of arithmetic operations: array operations and matrix operations. You can use these arithmetic operations to perform numeric computations, for example, adding two numbers, raising the elements of an array to a given power, or multiplying two matrices.
Matrix operations follow the rules of linear algebra. By contrast, array operations execute element by element operations and support 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 unnecessary.
Array operations work on corresponding elements of arrays with equal dimensions. For vectors, matrices, and multidimensional arrays, both operands must be the same size. Each element in the first input gets matched up with a similarly located element from the second input. If the inputs are different sizes, MATLAB cannot match the elements one-to-one.
As a simple example, you can add two vectors with the same length.
A = [1 1 1]
A = 1 1 1
B = 1:3
B = 1 2 3
ans = 2 3 4
If the vectors are not the same size you get an error.
B = 1:4
B = 1 2 3 4
Error using + Matrix dimensions must agree.
If one operand is a scalar and the other is not, then MATLAB applies the scalar to every element of the other operand. This property is known as scalar expansion because the scalar expands into an array of the same size as the other input, then the operation executes as it normally does with two arrays.
For example, the element-wise product of a scalar and a matrix uses scalar expansion.
A = [1 0 2;3 1 4]
A = 1 0 2 3 1 4
ans = 3 0 6 9 3 12
The following table provides a summary of array arithmetic operators in MATLAB. For function-specific information, click the link to the function reference page in the last column.
A+B adds A and B.
+A returns A.
A-B subtracts B from A
-A negates the elements of A.
A.*B is the element-by-element product of A and B.
A.^B is the matrix with elements A(i,j) to the B(i,j) power.
Right array division
A./B is the matrix with elements A(i,j)/B(i,j).
Left array division
A.\B is the matrix with elements B(i,j)/A(i,j).
A.' is the array transpose of A. For complex matrices, this does not involve conjugation.
Matrix operations follow the rules of linear algebra and are not compatible with multidimensional arrays. The required size and shape of the inputs in relation to one another depends on the operation. For nonscalar inputs, the matrix operators generally calculate different answers than their array operator counterparts.
For example, if you use the matrix right division operator, /, to divide two matrices, the matrices must have the same number of columns. But if you use the matrix multiplication operator, *, to multiply two matrices, then the matrices must have a common inner dimension. That is, the number of columns in the first input must be equal to the number of rows in the second input. The matrix multiplication operator calculates the product of two matrices with the formula,
To see this, you can calculate the product of two matrices.
A = [1 3;2 4]
A = 1 3 2 4
B = [3 0;1 5]
B = 3 0 1 5
ans = 6 15 10 20
The previous matrix product is not equal to the following element-wise product.
ans = 3 0 2 20
The following table provides a summary of matrix arithmetic operators in MATLAB. For function-specific information, click the link to the function reference page in the last column.
C = A*B is the linear algebraic product of the matrices A and B. The number of columns of A must equal the number of rows of B.
Matrix right division
x = B/A is the solution to the equation xA = B. Matrices A and B must have the same number of columns. In terms of the left division operator, B/A = (A'\B')'.
Matrix left division
x = A\B is the solution to the equation xA = B. Matrices A and B must have the same number of rows.
A^B is A to the power B, if B is a scalar. For other values of B, the calculation involves eigenvalues and eigenvectors.
Complex conjugate transpose
A' is the linear algebraic transpose of A. For complex matrices, this is the complex conjugate transpose.