## Matrix Operators

The following table gives a list of all arithmetic operators which work on matrices. They are divided into three categories:

The only new operator you are likely to see is the backslash or right divided operator. The expression a/b is equivalent to the operator b\a in Matlab.

### Matrix Operations

Each of these operators works pairs of matrices with appropriate dimensions or matrices and scalars.

- Matrix subtraction minus
* Matrix multiplication mtimes
^ Matrix exponentiation mpower
/ Right matrix division mrdivide
\ Left matrix division mldivide
' Hermitian transpose ctranspose
.' Transpose transpose

#### Plus +

If both operands are matrices of the same dimensions, the matrix entires are added elemement wise.

```>> [1 2; 3 4] + [2 3; 5 7]

ans =

3     5
8    11

```

If either operand is a scalar, that scalar is added to each entry element in the other matrix:

```>> [1 2; 3 4] + 5

ans =

6     7
8     9

```

In order to add a scalar r to the diagonal elements of a matrix A, use A + r*eye(size(A)).

#### Minus -

If both operands are matrices of the same dimensions, the matrix entires are added elemement wise.

```>> [1 2; 3 4] - [2 3; 5 7]

ans =

-1    -1
-2    -3

```

If the first operand is a scalar, each entry in the second matrix is subtracted from that scalar. If the second operand is a scalar, that scalar is subtracted from each element in the first matrix.

```>> 5 - [1 2; 3 4]

ans =

4     3
2     1

>> [1 2; 3 4] - 5

ans =

-4    -3
-2    -1

```

In order to subtract a scalar r from the diagonal elements of a matrix A, use A - r*eye(size(A)).

#### Matrix Times *

You can take the prodcut of two matrices A and B if the column dimension of the first matrix equals the row dimension of the second. That is, size( A, 2 ) == size( B, 1 ).

Matrix multiplication is defined such that given a column vector v with length equal to the row dimension of B, then we define A*B so that A*(B*v) == (A*B)*v.

```>> [1 2; 3 4] * [2 3; 5 7]

ans =

12    17
26    37

>> [1 2; 3 4] * [2 3]'

ans =

8
18

```

If either operand is a scalar, each entry in the other matrix is multiplied by that scalar.

#### Matrix Power ^

At least one operand must be a scalar, and any matrix must be square. I have not yet found a use for raising a scalar to a matrix.

```>> [1 2; 3 4]^3

ans =

37    54
81   118

>> [1 2; 3 4]^1.1

ans =

1.2742 - 0.0793i   2.3245 + 0.0363i
3.4867 + 0.0544i   4.7609 - 0.0249i

```

#### Matrix Right Divide /

A/B is equivalent to (B'\A')'. The column dimensions must be equal, that is, size( A, 2 ) == size( B, 2 ).

```>> [1 2; 3 4] / [2 3]

ans =

0.6154
1.3846

>> [1 2; 3 4] / [2 3; 5 7]

ans =

3.0000   -1.0000
-1.0000    1.0000

```

#### Matrix Left Divide \

A\B is equivalent to inv(A)*B. The row dimensions must be equal, that is, size( A, 1 ) == size( B, 1 ).

```>> [1 2; 3 4] \ [2; 3]

ans =

-1.0000
1.5000

>> [1 2; 3 4] \ [2 3; 5 7]

ans =

1.0000    1.0000
0.5000    1.0000

```

#### Complex Transpose '

The complex (or Hermitian) transpose operator ' transposes the the matrix and takes the complex conjugate of each entry in the transposed matrix. A' is equivalent to conj(A.').

If the matrix is real, this is equivalent to the standard matrix transpose.

```>> A = [1 2; 3 4; 5 6]

ans =

1     2
3     4
5     6

>> A'

ans =

1     3     5
2     4     6

>> A = rand(2) + rand(2)*i

ans =

0.9501 + 0.8913i   0.6068 + 0.4565i
0.2311 + 0.7621i   0.4860 + 0.0185i

>> A'

ans =

0.9501 - 0.8913i   0.2311 - 0.7621i
0.6068 - 0.4565i   0.4860 - 0.0185i

```

#### Transpose .'

The transpose operator .' takes an m × n matrix A and creates an n × m matrix B such that A(i, j) = B(j, i).

```>> A = [1 2; 3 4; 5 6]

ans =

1     2
3     4
5     6

>> A.'

ans =

1     3     5
2     4     6

```

### Element-wise Operations

Each of these operators works element wise. If either operand is a scalar r, that scalar is interpreted as the matrix r*ones(m, n) where m and n are the dimensions of the other operand.

.* Element-wise scalar multiplication times
.^ Element-wise scalar exponentiation power
./ Element-wise (right) scalar division rdivide
.\ Element-wise left scalar division ldivide

#### Times .*

If both operands are matrices of the same dimensions, the matrix entires are multiplied elemement wise.

```>> [1 2; 3 4] .* [2 3; 5 7]

ans =

2     6
15    28

```

This can be used, for example, in selection:

```>> A = [1 2 3; 4 5 6; 7 8 9];
>> B = [0 1 0; 1 0 1; 0 1 0];
>> A .* B

ans =

0        2        0
4        0        6
0        8        0

```

#### Power .^

If both operands are matrices of the same dimensions, the elements in the first matrix are raised to the power of the corresponding elements in the second matrix.

```>> [1 2; 3 4] .^ [2 3; 5 7]

ans =

1           8
243       16384

>> [1 2; 3 4] .^ 2

ans =

1     4
9    16

>> 2 .^ [2 3; 5 7]

ans =

4     8
32   128

```

#### Right Divide ./

If both operands are matrices of the same dimensions, the elements in the first matrix are divided by the corresponding elements in the second matrix.

```>> [1 2; 3 4] ./ [2 3; 5 7]

ans =

0.5000    0.6667
0.6000    0.5714

```

#### Left Divide .\

If both operands are matrices of the same dimensions, the elements in the second matrix are divided by the corresponding elements in the first matrix.

```>> [1 2; 3 4] .\ [2 3; 5 7]

ans =

2.0000    1.5000
1.6667    1.7500

```