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
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.
Right array division
Left array division
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.
Matrix right division
Matrix left division
Complex conjugate transpose