## 3. Matlab arithmetic operators

####
3.1 Matrix-vector operations

3.2 Array operations

3.3 Backslash operator

3.4 Complex arithmetic

### 3.1 Matrix-vector operations

The Matlab matrix vector arithmetic operations are addition (+)
subtraction (-) and multiplication(*). Addition and subtraction
are only defined if two matrices have the same dimensions. Multiplication
only works if the matrices have equal inner dimensions, i.e.
if A is an n by m matrix and B is an p by q matrix then A*B is
defined (and is calculated by Matlab) if m=p. Matlab also allows
for powers (^) of square matrices.

>> A = [ 1 2; 3 4], B = [5 6; 7 8]
A =
1 2
3 4
B =
5 6
7 8

>> A + B
ans =
6 8
10 12

>> A - B
ans =
-4 -4
-4 -4

>> A * B
ans =
19 22
43 50

>> x = [1 2], A*x
x =
1 2
??? Error using ==> *
Inner matrix dimensions must agree.

>> x = [1 2]', A*x
x =
1
2
ans =
5
11

### 3.2 Array operations

The Matlab array arithmetic operations are addition (+), subtraction (-),
array multiplication (.*), array division (./) and array power (.^).
These operations act element-wise on the arrays, for example if
A is an n by m matrix and B is an p by q matrix then A.*B is defined
only if n=p and m=q, and the (i,j) element of A.*B is the (i,j) element
of A multiplied by the (i,j) element of B.

>> A.*B
ans =
5 12
21 32

>> A.^2
ans =
1 4
9 16

>> A./B
ans =
0.2000 0.3333
0.4286 0.5000

### 3.3 Backslash operator \

The Matlab backslash operator solve linear systems of equations.
If you desire the solution of Ax = b, then the simplest method
using Matlab to find x is to set x = A\b. If A is an n by m matrix
and b is an p by q matrix then A\b is defined (and is calculated by
Matlab) if m=p. For non-square and singular systems, the operation
A\b gives the solution in the least squares sense.

>> A = [ 1 2; 3 4], x = [1 0]', A\x
A =
1 2
3 4
x =
1
0
ans =
-2.0000
1.5000

### 3.4 Complex arithmetic

Matlab is smart enough to know when numbers are complex, and how to
perform arithmetic on complex numbers. The variables i and j
can be used for the imaginary number sqrt(-1), unless they have
been previously defined. **If you have redefined i or j, in a loop
for example, then they are no longer equal to the square root of -1.
You have been warned.**

>> i
ans =
0 + 1.0000i

>> A = rand(2) + i*rand(2), x = [1 0]', A\x
A =
0.3028 + 0.3784i 0.1509 + 0.8537i
0.5417 + 0.8600i 0.6979 + 0.5936i
x =
1
0
ans =
0.8315 + 1.1217i
-0.5078 - 1.4635i
>> real(A)
ans =
0.3028 0.1509
0.5417 0.6979
>> imag(A)
ans =
0.3784 0.8537
0.8600 0.5936

*David Eyre*

*9/8/1998*