# Introduction

When working with matrices there are a number of element by element operations which as the name suggests work on an element by element basis. These differ from array operations which in contrast work over the whole array.

Element by element operations usually begin with a `.`

thus:

- Element by Element Addition
`.+`

- Element by Element Subtraction
`.-`

- Element by Element Multiplication
`.*`

(also known as the dot product) - Element by Element Division
`./`

- Element by Element Exponentiation
`.^`

Array operations do not begin with a `.`

thus:

- Array Addition
`+`

- Array Subtraction
`-`

- Array Multiplication
`*`

(also known as the dot product) - Array Division
`/`

(also known as the dot division) - Array Exponentiation
`^`

(also known as the dot exponentiation)

# Element by Element Addition

Array addition works in an identical manner to element by element addition therefore it does not matter if we use `+`

or `.+`

.

**Addition of Scalars**

Lets first look at the addition of scalars i.e. 1 by 1 matrices. Let's now for the sake of conceptualisation prescribe the values we have to objects. If we have 3 pens at home and order 5 more pens then when our order arrives we will have:

`3 pens + 5 pens = 8 pens`

To write this in Octave/MATLAB we would use

`3+5`

**Addition of Column Vectors**

Now let us conceptualise a more complicated example. Assume we have 3 pens and 2 pads at home, we then make a order for 5 more pens and 3 more pads.

Since pens and pads are separate objects we classify them as so. Treating each individual item type as a scalar:

`3 pens + 5 pens = 8 pens`

`2 pads + 3 pads = 5 pads`

If these are instead written as a column vector we will have:

To write this in Octave/MATLAB we would use

`[3;2]+[5;3]`

If I instead had 2 pens on my desk and I ordered 3 pads then in column vector notation I would have:

With this notation bare in mind that pens and pads are separate objects meaning a pen cannot transform into a pad and a pad cannot transform into a pen!

To write this in Octave/MATLAB we would use

`[2;0]+[0;3]`

**Note how we have to use zeros here to denote that we don't have a pen or pad in either column vector. Element by Element operations will only work if the Arrays have the same amount of Elements. **There are some exceptions which involve Scalar Expansion and Vector Expansion which will later be discussed.

**Addition of a Matrices**

Okay so far, so good. Let's make things slightly more complicated now. Assume there are two neighbours, neighbour 1 lives in the red house and he has 3 pens and 2 pads, he makes an order for 5 pens and 3 pads and neighbour 2 lives in the green house and she has 2 pens and 2 pads, she makes an order for 7 pens and 5 pads. In this case after the order the man in the red house would have 8 pens and 5 pads whilst the woman would have 9 pens and 7 pads.

To write this in Octave/MATLAB we would use

`[3,2;2,2]+[5,7;3,5]`

Now obviously we can conceive of more complicated scenarios where there are say 5 neighbours and they each have and order 7 different item types of stationary which would give a 7 by 5 matrix opposed to the 2 by 2 case above.

# Element by Element Subtraction

Array subtraction works in an identical manner to element by element subtraction therefore it does not matter if we use `-`

or `.-`

.

**Subtraction of Scalars**

The items we illustrated are consumables. Taking the case of scalars, in this case only looking at pens, assuming we have 8 pens and use 6 of them up writing then after we put the 6 used up pens in the recycle bin we are left with:

`8 pens - 6 pens = 2 pens`

To write this in Octave/MATLAB we would use

`8-6`

**Subtraction of Column Vectors**

If we started off with 8 pens and 5 pads and used up 6 pens and 3 pads then

Written as a column vector we will have:

To write this in Octave/MATLAB we would use

`[8;5]-[6;3]`

**Subtraction of a Matrices**

Okay so far, so good. Let's make things slightly more complicated again the man living in the red house has 8 pens and 5 pads and consumes 6 pens and 3 pads and the woman has 9 pens and 7 pads and consumes 8 pens and 4 pads. The man should therefore be left with 2 pens and 2 pads while the woman should be left with 1 pen and 3 pads

To write this in Octave/MATLAB we would use

`[8,9;5,7]-[6,8;3,4]`

# Element by Element Multiplication

Element by element multiplication (the dot product) `.*`

works differently from Array multiplication `*`

be careful not to confuse the two.

**Multiplication of Scalars**

Assuming one buys 5 pens and the price of a pen is £2. Then the amount of cash spent on pens is £10

`5 pens .* £2 = 5*£2 = £10`

To write this in Octave/MATLAB we would use

`5.*2`

Note for a 1 by 1 scalar array multiplication and element by element multiplication are identical.

**(Dot Product) of Column Vectors**

If we ordered 5 pens which cost £2 each and 3 pads which cost £6 each then

To write this in Octave/MATLAB we would use

`[5;3].*[2;6]`

**(Dot Product) of Matrices**

If the man in the red house bought 5 pens and 3 pads and the women in the green house bought 7 pens and 5 pads from the same shop costing £2 a pen and £6 a pad then:

To write this in Octave/MATLAB we would use

`[5,7;3,5].*[2,2;6,6]`

Alternatively if the woman living in the green house went to a more expensive shop where the price of pens and pads was £3 and £7 respectively

To write this in Octave/MATLAB we would use

`[5,7;3,5].*[2,3;6,7]`

# Element by Element Division

Element by element division (the dot division) `./`

works differently from Array multiplication `/`

be careful not to confuse the two.

**Division of Scalars**

When we looked at subtraction we seen that 6 pens were used. If this is over a period of 3 months, we may want to know the amount of pens used each month so we can make a monthly order. To do this we can use division.

`6 pens ./ 3 months = 6/3 pens/month = 2 pens/month`

To write this in Octave/MATLAB we would use

`6./3`

Note for a 1 by 1 scalar array division and element by element division are identical.

**(Dot Division) of a Column Vector**

If 6 pens and 3 pads were used up in 3 months then

To write this in Octave/MATLAB we would use

`[6;3]./[3;3]`

**(Dot Division) of Matrices**

If the man in the red house used 6 pens and 2 pads up in a period of 3 months and the women in the green house used up 8 pens and 6 pads in a period of 4 months then

To write this in Octave/MATLAB we would use

`[6,8;3,4]./[3,4;3,4]`

# Element by Element Operations Recap so Far…

For a 1 by 1 array i.e. a scalar element by element operations and array operations are identical as there is only 1 element.

For element by element addition `+`

is identical to array addition `.+`

and element by element subtraction `-`

is identical to array subtraction `.-`

.

To check the dimensions of a Matrix M we can use the function size.

`[m1,n1]=size(M)`

`[m2,n2]=size(N)`

For all element by element operations the Matrix Dimensions must match, i.e. `m1=m2`

and `n1=n2`

. If we take

`O=M+N`

`P=M-N`

`Q=M.*N`

`R=M./N`

The dimensions of the matrix after carrying element by element operations will also match:

`[m3,n3]=size(O)`

`[m4,n4]=size(P)`

`[m5,n5]=size(Q)`

`[m6,n6]=size(R)`

i.e. `m1=m2=m3=m4=m5=m6`

and `n1=n2=n3=n4=n5=n6`

.

The Dot Product can be thought of as an array giving the sum of the cost of each item type a person has bought e.g. for a single person the price they spent on pens and the price they spent on pads:

For 2 people the price each person spent on pens and the price each person spent on pads.

# Array Multiplication

For array operations there are a different set of rules. In the example listed above, we can set up Matrix multiplication to give the sum of the order. For matrix multiplication the inner dimensions must match i.e. for Matrix M (m1,n1) and Matrix N (m2,n2) in order for matrix multiplication to be valid n1 and m2 must match. When these matrices are multiplied P=M*N will have dimensions matching the outer dimensions of M*N i.e. m1 and n2. Thus in the example below the 1 by 2 matrix multiplied by the 2 by 1 matrix gives a 1 by 1 matrix and multiplication is allowed as the inner dimensions match.

In this context the units of pens and pads don't matter as we are just wanting to know the quantity of each item and calculate the total amount of money spent on the order. To write this in Octave/MATLAB we would use:

`[2,6]*[5;3]`

In the example below the 2 by 2 matrix multiplied by the 2 by 2 matrix gives a 2 by 2 matrix and multiplication is allowed as the inner dimensions match.

This 2 by 2 matrix contains the total prices the man in the spends as the first element in the 1st row and 1st column and the amount the woman spends in the 2nd row and 2nd column. The other two values in the matrix 1st column and 2nd row are the amount of money the man would have spent had he went to the same shops and paid the same prices per item as the woman and conversely in the 2nd column and 1st row the amount of money the woman would have paid, had she went to the same shops as the man and paid the same prices as the man.

To write this in Octave/MATLAB we would use:

`[2,6;3,7]*[5,7;3,5]`

Okay so far we have demonstrated matrix multiplication by using:

- M (2 by 2) and N (2 by 2) giving P (2 by 2)
- M (1 by 2) and N (2 by 1) giving P (1 by 1)

In these cases the product matrix contained less elements or less information than the original matrices being multiplied, this is because for the product matrix, we were essentially interested in the total price paid and not the price of each item.

In other cases, it is possible for the Product Matrix to contain more elements than the starting Matrices. Lets look at a different example. Assume you work for the Fire Service and are in charge of equipment provision. You designate, that each Fireman requires:

- 1 Helmet
- 5 Sets of Uniform
- 1 Axe
- 2 Sets of Boots

You are in charge of a single Station with:

- 3 Firemen

In this case you can list the equipment as a 4 by 1 column vector and the number of firemen as a 1 by scalar. Thus in the example below the 4 by 1 column vector multiplied by the 1 by 1 scalar gives a 4 by 1 column vector and multiplication is allowed as the inner dimensions match. The resulting product lists the total number of each piece of equipment that is required at the Station:

To write this in Octave/MATLAB we would use:

`[1;5;1;2]*[3]`

Now supposing you were promoted and this time you were in charge of three Stations. Station 1 has 3 Firemen, Station 2 has 5 Firemen and Station 3 has 4 Firemen. Then you once again designate, that each Fireman requires:

- 1 Helmet
- 5 Sets of Uniform
- 1 Axe
- 2 Sets of Boots

You are in charge of the Stations with:

- 3 Firemen, 5 Firemen, 4 Firemen

In this case you can list the equipment as a 4 by 1 column vector and the number of firemen as a 1 by 3 row vector. Thus in the example below the 4 by 1 column vector multiplied by the 1 by 3 row vector gives a 4 by 3 matrix and multiplication is allowed as the inner dimensions match. The resulting product lists the total number of each piece of equipment that is required at each Station:

To write this in Octave/MATLAB we would use:

`[1;5;1;2]*[3,5,4]`

We can look at a slightly more complicated example now, assume that you have managed the three Fire Stations for two years. After the first year, you got feedback from your squad that the boots don't last as long as you thought they would, so decided to up the boot inventory for the second year. This means you have a 4 by 2 matrix of equipment required for a Fireman. i.e. a list of inventory required with each column corresponding to a year. Also, due to demand, the number of Firemen increases from 3 to 4 in Station 1 and from 5 to 6 in Station 2, this gives a 2 by 3 matrix for the number of Firemen required at each station with each row corresponding to a year. Thus in the example below the 4 by 2 matrix multiplied by the 2 by 3 matrix gives a 4 by 3 matrix and multiplication is allowed as the inner dimensions match. The resulting product lists the total number of each piece of equipment that is required at each Station:

Here the product matrix contains a list of the equipment required for each Station but it does not care what year the equipment was bought in (i.e. the information explicitly stated in the inner dimensions of the matrices undergoing multiplication). To write this in Octave/MATLAB we would use:

`[1,1;5,5;1,1;2,3]*[3,5,4;4,6,4]`

# Array Operations Recap so Far…

For a 1 by 1 array i.e. a scalar element by element operations and array operations are identical as there is only 1 element.

For element by element addition `+`

is identical to array addition `.+`

and element by element subtraction `-`

is identical to array subtraction `.-`

.

To check the dimensions of a Matrix M we can use the function size.

`[m1,n1]=size(M)`

`[m2,n2]=size(N)`

For array addition and array subtraction operations the Matrix Dimensions must match, i.e. `m1=m2`

and `n1=n2`

. If we take

`O=M+N`

`P=M-N`

The dimensions of the matrix after carrying element by element operations will also match:

`O=M+N`

`P=M-N`

The dimensions of the matrix after carrying element by element operations will also match:

`[m3,n3]=size(O)`

`[m4,n4]=size(P)`

i.e. `m1=m2=m3=m4`

and `n1=n2=n3=n4`

.

For array multiplication:

`[m1,n1]=size(M)`

`[m2,n2]=size(N)`

The inner dimensions of the two matrices being multiplied must match i.e. `n1=m2`

.

# Exponentiation

## Element Exponentiation

For element by element exponentiation we follow the normal rules for element by element operations. Suppose we have two sets of cubes, a red set and a blue set and we know the lengths. We can use element by element exponentiation to calculate the area of a face and the volume of the cube:

To write this in Octave/MATLAB we would use:

`Lengths = [1,2,3;2,4,8]`

`Areas = [1,2,3;2,4,8].^2`

`Areas = [1,2,3;2,4,8].*[1,2,3;2,4,8]`

`Volumes = [1,2,3;2,4,8].^3`

`Volumes = [1,2,3;2,4,8].*[1,2,3;2,4,8].*[1,2,3;2,4,8]`

## Array Exponentiation

For Array Exponentiation.

`B=A^2`

`B=A*A`

`C=A^3`

`C=A*A*A`

For array multiplication, to be valid i.e. `A*A`

the following condition must be met `n1=m2`

. Now for array exponentiation, the two arrays being multiplied are the same thus `n1=m1`

and `n2=m2`

and because `n1=m2`

the only condition which satisfies the rule of matrix multiplication is that of a square matrix i.e. `m1=m2`

.

`A=[1,2;3,4]`

B=[1,2;3,4]^2

B=[1,2;3,4]*[1,2;3,4]

C=[1,2;3,4]^3

C=[1,2;3,4]*[1,2;3,4]*[1,2;3,4]

# Matrix Division

Matrix Division is always gently brushed over at school with only the special case of a 2 by 2 square matrix being solved using the inverse of the square matrix. For:

`A*B=C`

With AInv, the Inverse Matrix of A is defined as:

Substituting in the numbers:

In Octave/MATLAB to find the Inv Matrix we can use the function `inv( )`

:

`AInv=AInv(A)`

Now when a Matrix is multiplied by it's inverse will give the Identity matrix I:

`AInv*A=A*AInv=I`

If we multiply the expression

`A*B=C`

throughout by the inverse of A:

`AInv*A*B=AInv*C`

`I*B=AInv*C`

When a Matrix is multiplied by the Identity Matrix it is unchanged

Therefore:

`B=AInv*C`

This gives the correct value of B.

This can be thought of as:

`A(1,1)*B(1,1)+A(1,2)*B(2,1)=C(1,1)`

A(1,1)*B(1,2)+A(1,2)*B(2,2)=C(1,2)

A(2,1)*B(1,1)+A(2,2)*B(2,1)=C(2,1)

A(2,1)*B(1,2)+A(2,2)*B(2,2)=C(2,2)

Assuming we don't know the values of B, but we know the values of A and C. This is 4 sets of equations, containing 4 unknowns:

`B(1,1)+2*B(2,1)=19`

`B(1,2)+2*B(2,2)=22`

`3*B(1,1)+4*B(2,1)=43`

`3*B(1,2)+4*B(2,2)=50`

Rearranging 1:

`B(1,1)+2*B(2,1)=19`

B(1,1)=19-2*B(2,1)

Substituting into 3:

`3*(19-2*B(2,1))+4*B(2,1)=43`

57-6*B(2,1)+4*B(2,1)=43

-2*B(2,1)=43-57

-2*B(2,1)=-14

B(2,1)=-14/-2

B(2,1)=7

Substituting this into 1 (which was rearranged):

`B(1,1)=19-2*B(2,1)`

`B(1,1)=19-2*7`

`B(1,1)=19-14`

`B(1,1)=5`

Now rearranging 2:

`B(1,2)+2*B(2,2)=22`

`B(1,2)=22-2*B(2,2)`

Now substituting into 4:

`3*B(1,2)+4*B(2,2)=50`

`3*(22-2*B(2,2))+4*B(2,2)=50`

`66-6*B(2,2)+4*B(2,2)=50`

`-6*B(2,2)+4*B(2,2)=50-66`

`-2*B(2,2)=-16`

`B(2,2)=-16/-2`

`B(2,2)=8`

Substituting this into the rearranged 2:

`B(1,2)=22-2*B(2,2)`

`B(1,2)=22-2*8`

`B(1,2)=22-16`

`B(1,2)=6`

This gives:

`B(1,1)=5`

`B(1,2)=6`

`B(2,1)=7`

`B(2,2)=8`

As expected.

Octave/MATLAB can perform Matrix Division. Let's look at the case:

`A*B=C`

Because A*B≠B*A the notation to solve for A given B and C, or B given A and C is distinct.

Right division is used to calculate A given B and C:

`A1=C/B`

It is useful to think of this in terms of the square matrices given above where

`A1*B=C`

`A1*B*inv(B)=C*inv(B)`

`A1=C*inv(B)`

With the inverse being on the right hand side (multiplied throughout so it is beside B) so right division is used.

Left division is used to calculate B given A and C:

`B1=A\C`

It is likewise useful to think of this as

`A*B1=C`

`inv(A)*A*B1=inv(A)*C`

`B1=inv(A)*C`

with the inverse being on the left hand side (multiplied throughout so in is beside A) so left division is used.

Thinking of the left and right divisions in terms of where you would put the inverse helps you put the variables and the left or right division sign in the correct place.

We can use the data we multiplied together to work back from to illustrate division:

Assuming we know S and A and want to calculate E:

`E1=A/S`

`A1=E\A`