## Array vs. Matrix Operations

### Introduction

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

Array operations execute element by element operations on corresponding elements of vectors, matrices, and multidimensional arrays. If the operands have the same size, then each element in the first operand gets matched up with the element in the same location in the second operand. If the operands have compatible sizes, then each input is implicitly expanded as needed to match the size of the other. For more information, see Compatible Array Sizes for Basic Operations.

As a simple example, you can add two vectors with the same size.

A = [1 1 1]
A =

1     1     1
B = [1 2 3]
B =

1     2     3
A+B
ans =

2     3     4

If one operand is a scalar and the other is not, then MATLAB implicitly expands the scalar to be the same size as the other operand. For example, you can compute the element-wise product of a scalar and a matrix.

A = [1 2 3; 1 2 3]
A =

1     2     3
1     2     3
3.*A
ans =

3     6     9
3     6     9

Implicit expansion also works if you subtract a 1-by-3 vector from a 3-by-3 matrix because the two sizes are compatible. When you perform the subtraction, the vector is implicitly expanded to become a 3-by-3 matrix.

A = [1 1 1; 2 2 2; 3 3 3]
A =

1     1     1
2     2     2
3     3     3
m = [2 4 6]
m =

2     4     6
A - m
ans =

-1    -3    -5
0    -2    -4
1    -1    -3

A row vector and a column vector have compatible sizes. If you add a 1-by-3 vector to a 2-by-1 vector, then each vector implicitly expands into a 2-by-3 matrix before MATLAB executes the element-wise addition.

x = [1 2 3]
x =

1     2     3
y = [10; 15]
y =

10
15
x + y
ans =

11    12    13
16    17    18

If the sizes of the two operands are incompatible, then you get an error.

A = [8 1 6; 3 5 7; 4 9 2]
A =

8     1     6
3     5     7
4     9     2
m = [2 4]
m =

2     4
A - m
Matrix dimensions must agree.

The following table provides a summary of arithmetic array operators in MATLAB. For function-specific information, click the link to the function reference page in the last column.

Operator

Purpose

Description

Reference Page

+

plus

+

Unary plus

+A returns A.

uplus

-

Subtraction

A-B subtracts B from A

minus

-

Unary minus

-A negates the elements of A.

uminus

.*

Element-wise multiplication

A.*B is the element-by-element product of A and B.

times

.^

Element-wise power

A.^B is the matrix with elements A(i,j) to the B(i,j) power.

power
./

Right array division

A./B is the matrix with elements A(i,j)/B(i,j).

rdivide

.\

Left array division

A.\B is the matrix with elements B(i,j)/A(i,j).

ldivide

.'

Array transpose

A.' is the array transpose of A. For complex matrices, this does not involve conjugation.

transpose

### Matrix Operations

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,

$C\left(i,j\right)=\sum _{k=1}^{n}A\left(i,k\right)B\left(k,j\right).$

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
A*B
ans =

6    15
10    20

The previous matrix product is not equal to the following element-wise product.

A.*B
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.

Operator

Purpose

Description

Reference Page

*

Matrix multiplication

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.

mtimes

\

Matrix left division

x = A\B is the solution to the equation Ax = B. Matrices A and B must have the same number of rows.

mldivide

/

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')'.

mrdivide

^

Matrix power

A^B is A to the power B, if B is a scalar. For other values of B, the calculation involves eigenvalues and eigenvectors.

mpower

'

Complex conjugate transpose

A' is the linear algebraic transpose of A. For complex matrices, this is the complex conjugate transpose.

ctranspose