# 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

<span style="color: #ff0000;">.</span>

thus:

<span style="color: #3366ff;"><span style="color: #ff0000;">.</span>+</span>
• Element by Element Subtraction
<span style="color: #3366ff;"><span style="color: #ff0000;">.</span>-</span>

• Element by Element Multiplication
<span style="color: #ff0000;">.</span><span style="color: #3366ff;">*</span>

(also known as the dot product)

• Element by Element Division
<span style="color: #ff0000;">.</span><span style="color: #3366ff;">/</span>
• Element by Element Exponentiation
<span style="color: #ff0000;">.</span><span style="color: #3366ff;">^</span>

Array operations do not begin with a

<span style="color: #ff0000;">.</span>

thus:

<span style="color: #3366ff;">+</span>
• Array Subtraction
<span style="color: #3366ff;">-</span>

• Array Multiplication
<span style="color: #3366ff;">*</span>

(also known as the dot product)

• Array Division
<span style="color: #3366ff;">/</span>

(also known as the dot division)

• Array Exponentiation
<span style="color: #3366ff;">^</span>

(also known as the dot exponentiation)

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

<span style="color: #3366ff;">+</span>

or

<span style="color: #ff0000;">.</span><span style="color: #3366ff;">+</span>

.

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 <span style="color: #3366ff;">pens</span> + 5 <span style="color: #3366ff;">pens</span> = 8 <span style="color: #3366ff;">pens</span> $\displaystyle \left[ {\text{3 pens}} \right]+\left[ {\text{5 pens}} \right]=\left[ {8\text{ pens}} \right]$ $\displaystyle \left[ 3 \right]+\left[ 5 \right]=\left[ 8 \right]$ $\displaystyle 3+5=8$

To write this in Octave/MATLAB we would use

3+5

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:

<span style="background-color: #f2f4f5; color: #222222;">3 </span><span style="color: #3366ff;">pens</span><span style="background-color: #f2f4f5; color: #222222;"> + 5 </span><span style="color: #3366ff;">pens</span><span style="background-color: #f2f4f5; color: #222222;"> = 8 </span><span style="color: #3366ff;">pens</span> $\displaystyle \left[ {\text{3 pens}} \right]+\left[ {\text{5 pens}} \right]=\left[ {8\text{ pens}} \right]$ $\displaystyle \left[ 3 \right]+\left[ 5 \right]=\left[ 8 \right]$ $\displaystyle 3+5=8$

<span style="background-color: #f2f4f5; color: #222222;">2 </span><span style="color: #ff0000;">pads</span><span style="background-color: #f2f4f5; color: #222222;"> + 3 </span><span style="color: #ff0000;">pads</span><span style="background-color: #f2f4f5; color: #222222;"> = 5 </span><span style="color: #ff0000;">pads</span> $\displaystyle \left[ {\text{2 pads}} \right]+\left[ {\text{3 pads}} \right]=\left[ {5\text{ pads}} \right]$ $\displaystyle \left[ 2 \right]+\left[ 3 \right]=\left[ 5 \right]$ $\displaystyle 2+3=5$

If these are instead written as a column vector we will have: $\displaystyle \left[ {\begin{array}{*{20}{c}} {3\text{ pens}} \\ {2\text{ pads}} \end{array}} \right]+\left[ {\begin{array}{*{20}{c}} {\text{5 pens}} \\ {3\text{ pads}} \end{array}} \right]=\left[ {\begin{array}{*{20}{c}} {8\text{ pens}} \\ {5\text{ pads}} \end{array}} \right]$ $\displaystyle \left[ {\begin{array}{*{20}{c}} 3 \\ 2 \end{array}} \right]+\left[ {\begin{array}{*{20}{c}} 5 \\ 3 \end{array}} \right]=\left[ {\begin{array}{*{20}{c}} 8 \\ 5 \end{array}} \right]$

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:  $\displaystyle \left[ {\begin{array}{*{20}{c}} {\text{2 pens}} \\ {0\text{ pads}} \end{array}} \right]+\left[ {\begin{array}{*{20}{c}} {\text{0 pens}} \\ {3\text{ pads}} \end{array}} \right]=\left[ {\begin{array}{*{20}{c}} {2\text{ pens}} \\ {3\text{ pads}} \end{array}} \right]$

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! $\displaystyle \left[ {\begin{array}{*{20}{c}} 2 \\ 0 \end{array}} \right]+\left[ {\begin{array}{*{20}{c}} 0 \\ 3 \end{array}} \right]=\left[ {\begin{array}{*{20}{c}} 2 \\ 3 \end{array}} \right]$

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.

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.  $\displaystyle \left[ {\begin{array}{*{20}{c}} {\text{3 pens}} & {2\text{ pens}} \\ {\text{2 pads}} & {\text{2 pads}} \end{array}} \right]+\left[ {\begin{array}{*{20}{c}} {\text{5 pens}} & {\text{7 pens}} \\ {\text{3 pads}} & {5\text{ pads}} \end{array}} \right]=\left[ {\begin{array}{*{20}{c}} {\text{8 pens}} & {\text{9 pens}} \\ {5\text{ pads}} & {7\text{ pads}} \end{array}} \right]$ $\displaystyle \left[ {\begin{array}{*{20}{c}} 3 & 2 \\ 2 & 2 \end{array}} \right]+\left[ {\begin{array}{*{20}{c}} 5 & 7 \\ 3 & 5 \end{array}} \right]=\left[ {\begin{array}{*{20}{c}} 8 & 9 \\ 5 & 7 \end{array}} \right]$

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

<span style="color: #3366ff;">-</span>

or

<span style="color: #ff0000;">.</span><span style="color: #3366ff;">-</span>

.

## 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 <span style="color: #3366ff;">pens</span> - 6 <span style="color: #3366ff;">pens</span> = 2 <span style="color: #3366ff;">pens</span> $\displaystyle \left[ {\text{8 pens}} \right]-\left[ {\text{6 pens}} \right]=\left[ {2\text{ pens}} \right]$ $\displaystyle \left[ 8 \right]-\left[ 6 \right]=\left[ 2 \right]$ $\displaystyle 8-6=8$

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: $\displaystyle \left[ {\begin{array}{*{20}{c}} {\text{8 pens}} \\ {\text{5 pads}} \end{array}} \right]-\left[ {\begin{array}{*{20}{c}} {\text{6 pens}} \\ {3\text{ pads}} \end{array}} \right]=\left[ {\begin{array}{*{20}{c}} {2\text{ pens}} \\ {2\text{ pads}} \end{array}} \right]$ $\displaystyle \left[ {\begin{array}{*{20}{c}} 8 \\ 5 \end{array}} \right]-\left[ {\begin{array}{*{20}{c}} 6 \\ 3 \end{array}} \right]=\left[ {\begin{array}{*{20}{c}} 2 \\ 2 \end{array}} \right]$

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  $\displaystyle \left[ {\begin{array}{*{20}{c}} {\text{8 pens}} & {9\text{ pens}} \\ {\text{5 pads}} & {\text{7 pads}} \end{array}} \right]+\left[ {\begin{array}{*{20}{c}} {\text{6 pens}} & {\text{8 pens}} \\ {\text{3 pads}} & {4\text{ pads}} \end{array}} \right]=\left[ {\begin{array}{*{20}{c}} {\text{2 pens}} & {\text{1 pens}} \\ {2\text{ pads}} & {3\text{ pads}} \end{array}} \right]$ $\displaystyle \left[ {\begin{array}{*{20}{c}} 8 & 9 \\ 5 & 7 \end{array}} \right]+\left[ {\begin{array}{*{20}{c}} 6 & 8 \\ 3 & 4 \end{array}} \right]=\left[ {\begin{array}{*{20}{c}} 2 & 1 \\ 2 & 3 \end{array}} \right]$

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)

<span style="color: #3366ff;"><span style="color: #ff0000;">.</span>*</span>

works differently from Array multiplication

<span style="color: #3366ff;">*</span>

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 <span style="color: #ff0000;">.</span>* £2 = 5*£2 = £10 $\displaystyle \left[ 5 \right].*\left[ {\pounds2} \right]=\left[ {5*\pounds2} \right]=\left[ {\pounds10} \right]$

To write this in Octave/MATLAB we would use

5<span style="color: #ff0000;">.</span>*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  $\displaystyle \left[ {\begin{array}{*{20}{c}} 5 \\ 3 \end{array}} \right].*\left[ {\begin{array}{*{20}{c}} {\pounds2} \\ {\pounds6} \end{array}} \right]=\left[ {\begin{array}{*{20}{c}} {5*\pounds2} \\ {3*\pounds6} \end{array}} \right]=\left[ {\begin{array}{*{20}{c}} {\pounds10} \\ {\pounds18} \end{array}} \right]$ $\displaystyle \left[ {\begin{array}{*{20}{c}} 5 \\ 3 \end{array}} \right].*\left[ {\begin{array}{*{20}{c}} 2 \\ 6 \end{array}} \right]=\left[ {\begin{array}{*{20}{c}} {5*2} \\ {3*6} \end{array}} \right]=\left[ {\begin{array}{*{20}{c}} {10} \\ {18} \end{array}} \right]$

To write this in Octave/MATLAB we would use

[5;3]<span style="color: #ff0000;">.</span>*[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:  $\displaystyle \left[ {\begin{array}{*{20}{c}} {5\text{ pens}} & {7\text{ pens}} \\ {3\text{ pads}} & {5\text{ pads}} \end{array}} \right].*\left[ {\begin{array}{*{20}{c}} {\pounds2} & {\pounds2} \\ {\pounds6} & {\pounds6} \end{array}} \right]=\left[ {\begin{array}{*{20}{c}} {5*\pounds2} & {7*\pounds2} \\ {3*\pounds6} & {5*\pounds6} \end{array}} \right]=\left[ {\begin{array}{*{20}{c}} {10} & {14} \\ {18} & {30} \end{array}} \right]$ $\displaystyle \left[ {\begin{array}{*{20}{c}} 5 & 7 \\ 3 & 5 \end{array}} \right].*\left[ {\begin{array}{*{20}{c}} 2 & 2 \\ 6 & 6 \end{array}} \right]=\left[ {\begin{array}{*{20}{c}} {\pounds10} & {\pounds14} \\ {\pounds18} & {\pounds30} \end{array}} \right]$

To write this in Octave/MATLAB we would use

[5,7;3,5]<span style="color: #ff0000;">.</span>*[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  $\displaystyle \left[ {\begin{array}{*{20}{c}} {5\text{ pens}} & {7\text{ pens}} \\ {3\text{ pads}} & {5\text{ pads}} \end{array}} \right].*\left[ {\begin{array}{*{20}{c}} {\pounds2} & {\pounds3} \\ {\pounds6} & {\pounds7} \end{array}} \right]=\left[ {\begin{array}{*{20}{c}} {5*\pounds2} & {7*\pounds3} \\ {3*\pounds6} & {5*\pounds7} \end{array}} \right]=\left[ {\begin{array}{*{20}{c}} {\pounds10} & {\pounds21} \\ {\pounds18} & {\pounds35} \end{array}} \right]$ $\displaystyle \left[ {\begin{array}{*{20}{c}} 5 & 7 \\ 3 & 5 \end{array}} \right].*\left[ {\begin{array}{*{20}{c}} 2 & 3 \\ 6 & 7 \end{array}} \right]=\left[ {\begin{array}{*{20}{c}} {10} & {21} \\ {18} & {35} \end{array}} \right]$

To write this in Octave/MATLAB we would use

[5,7;3,5]<span style="color: #ff0000;">.</span>*[2,3;6,7]

# Element by Element Division

Element by element division (the dot division)

<span style="color: #3366ff;"><span style="color: #ff0000;">.</span>/</span>

works differently from Array multiplication

<span style="color: #3366ff;">/</span>

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 <span style="color: #ff0000;">.</span>/ 3 months = 6/3 pens/month = 2 pens/month $\displaystyle \left[ {6\text{ pens}} \right]./\left[ {\text{3 months}} \right]=\left[ {6\text{ pens}/3\text{ months}} \right]=\left[ {2\text{ pens/month}} \right]$ $\displaystyle \left[ 6 \right]./\left[ 3 \right]=\left[ {6/3} \right]=\left[ 2 \right]$

To write this in Octave/MATLAB we would use

6<span style="color: #ff0000;">.</span>/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  $\displaystyle \left[ {\begin{array}{*{20}{c}} {6\text{ pens}} \\ {3\text{ pads}} \end{array}} \right]./\left[ {\begin{array}{*{20}{c}} {3\text{ months}} \\ {3\text{ months}} \end{array}} \right]=\left[ {\begin{array}{*{20}{c}} {6/3\text{ pens/month}} \\ {3/3\text{ pads/month}} \end{array}} \right]=\left[ {\begin{array}{*{20}{c}} {2\text{ pens/month}} \\ {2\text{ pads/month}} \end{array}} \right]$ $\displaystyle \left[ {\begin{array}{*{20}{c}} 6 \\ 3 \end{array}} \right]./\left[ {\begin{array}{*{20}{c}} 3 \\ 3 \end{array}} \right]=\left[ {\begin{array}{*{20}{c}} {6/3} \\ {3/3} \end{array}} \right]=\left[ {\begin{array}{*{20}{c}} 2 \\ 1 \end{array}} \right]$

To write this in Octave/MATLAB we would use

[6;3]<span style="color: #ff0000;">.</span>/[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  $\displaystyle \left[ {\begin{array}{*{20}{c}} {6\text{ pens}} & {\text{8 pens}} \\ {3\text{ pads}} & {4\text{ pads}} \end{array}} \right]./\left[ {\begin{array}{*{20}{c}} {3\text{ months}} & {4\text{ months}} \\ {3\text{ months}} & {4\text{ months}} \end{array}} \right]=\left[ {\begin{array}{*{20}{c}} {6/3\text{ pens/month}} & {8/4\text{ pens/month}} \\ {3/3\text{ pads/month}} & {4/4\text{ pads/month}} \end{array}} \right]=\left[ {\begin{array}{*{20}{c}} {2\text{ pens/month}} & {2\text{ pens/month}} \\ {1\text{ pads/month}} & {2\text{ pads/month}} \end{array}} \right]$ $\displaystyle \left[ {\begin{array}{*{20}{c}} 6 & 8 \\ 3 & 4 \end{array}} \right]./\left[ {\begin{array}{*{20}{c}} 3 & 4 \\ 3 & 4 \end{array}} \right]=\left[ {\begin{array}{*{20}{c}} {6/3} & {8/4} \\ {3/3} & {4/4} \end{array}} \right]=\left[ {\begin{array}{*{20}{c}} 2 & 2 \\ 1 & 1 \end{array}} \right]$

To write this in Octave/MATLAB we would use

[6,8;3,4]<span style="color: #ff0000;">.</span>/[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.

+

<span style="color: #ff0000;">.</span>+

and element by element subtraction

-

is identical to array subtraction

<span style="color: #ff0000;">.</span>-

.

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

[<span style="color: #00ccff;">m1</span>,<span style="color: #ffcc00;">n1</span>]=<span style="color: #3366ff;">size(</span>M<span style="color: #3366ff;">)</span>
[<span style="color: #00ccff;">m2</span>,<span style="color: #ffcc00;">n2</span>]=<span style="color: #3366ff;">size(</span>N<span style="color: #3366ff;">)</span>

For all element by element operations the Matrix Dimensions must match, i.e.

<span style="color: #00ccff;">m1</span>=<span style="color: #00ccff;">m2</span>

and

<span style="color: #ffcc00;">n1</span>=<span style="color: #ffcc00;">n2</span>

. If we take

O=M+N
P=M-N
Q=M<span style="color: #ff0000;">.</span>*N
R=M<span style="color: #ff0000;">.</span>/N

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

<span style="color: #222222; font-family: monospace; background-color: #e9ebec;">[</span><span style="color: #00ccff;">m3</span><span style="color: #222222; font-family: monospace; background-color: #e9ebec;">,</span><span style="color: #ffcc00;">n3</span><span style="color: #222222; font-family: monospace; background-color: #e9ebec;">]=</span><span style="color: #3366ff;">size(</span><span style="color: #222222; font-family: monospace; background-color: #e9ebec;">O</span><span style="color: #3366ff;">)</span>
[<span style="color: #00ccff;">m4</span>,<span style="color: #ffcc00;">n4</span>]=<span style="color: #3366ff;">size(</span>P<span style="color: #3366ff;">)</span>
[<span style="color: #00ccff;">m5</span>,<span style="color: #ffcc00;">n5</span>]=<span style="color: #3366ff;">size(</span>Q<span style="color: #3366ff;">)</span>
[<span style="color: #00ccff;">m6</span>,<span style="color: #ffcc00;">n6</span>]=<span style="color: #3366ff;">size(</span>R<span style="color: #3366ff;">)</span>

i.e.

<span style="color: #00ccff;">m1</span>=<span style="color: #00ccff;">m2<span style="color: #000000;">=</span>m3<span style="color: #000000;">=</span>m4<span style="color: #000000;">=</span>m5<span style="color: #000000;">=</span>m6</span>

and

<span style="color: #ffcc00;">n1</span>=<span style="color: #ffcc00;">n2<span style="color: #000000;">=</span>n3<span style="color: #000000;">=</span>n4<span style="color: #000000;">=</span>n5<span style="color: #000000;">=</span>n6</span>

.

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]*

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.

+

<span style="color: #ff0000;">.</span>+

and element by element subtraction

-

is identical to array subtraction

<span style="color: #ff0000;">.</span>-

.

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

[<span style="color: #00ccff;">m1</span>,<span style="color: #ffcc00;">n1</span>]=<span style="color: #3366ff;">size(</span>M<span style="color: #3366ff;">)</span>
[<span style="color: #00ccff;">m2</span>,<span style="color: #ffcc00;">n2</span>]=<span style="color: #3366ff;">size(</span>N<span style="color: #3366ff;">)</span>

For array addition and array subtraction operations the Matrix Dimensions must match, i.e.

<span style="color: #00ccff;">m1</span>=<span style="color: #00ccff;">m2</span>

and

<span style="color: #ffcc00;">n1</span>=<span style="color: #ffcc00;">n2</span>

. 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:

<span style="color: #222222; font-family: monospace; background-color: #e9ebec;">[</span><span style="color: #00ccff;">m3</span><span style="color: #222222; font-family: monospace; background-color: #e9ebec;">,</span><span style="color: #ffcc00;">n3</span><span style="color: #222222; font-family: monospace; background-color: #e9ebec;">]=</span><span style="color: #3366ff;">size(</span><span style="color: #222222; font-family: monospace; background-color: #e9ebec;">O</span><span style="color: #3366ff;">)</span>
[<span style="color: #00ccff;">m4</span>,<span style="color: #ffcc00;">n4</span>]=<span style="color: #3366ff;">size(</span>P<span style="color: #3366ff;">)</span>

i.e.

<span style="color: #00ccff;">m1</span>=<span style="color: #00ccff;">m2<span style="color: #000000;">=</span>m3<span style="color: #000000;">=</span>m4</span>

and

<span style="color: #ffcc00;">n1</span>=<span style="color: #ffcc00;">n2<span style="color: #000000;">=</span>n3<span style="color: #000000;">=</span>n4</span>

.

For array multiplication:

[<span style="color: #00ccff;">m1</span>,<span style="color: #ffcc00;">n1</span>]=<span style="color: #3366ff;">size(</span>M<span style="color: #3366ff;">)</span>
[<span style="color: #00ccff;">m2</span>,<span style="color: #ffcc00;">n2</span>]=<span style="color: #3366ff;">size(</span>N<span style="color: #3366ff;">)</span>

The inner dimensions of the two matrices being multiplied must match i.e.

<span style="color: #ffcc00;">n1</span>=<span style="color: #00ccff;">m2</span>

.

# 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]<span style="color: #ff0000;">.</span>^2
Areas = [1,2,3;2,4,8]<span style="color: #ff0000;">.</span>*[1,2,3;2,4,8]
Volumes = [1,2,3;2,4,8]<span style="color: #ff0000;">.</span>^3
Volumes = [1,2,3;2,4,8]<span style="color: #ff0000;">.</span>*[1,2,3;2,4,8]<span style="color: #ff0000;">.</span>*[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

<span style="color: #ffcc00;">n1</span>=<span style="color: #00ccff;">m2</span>

. Now for array exponentiation, the two arrays being multiplied are the same thus

<span style="color: #ffcc00;">n1</span>=<span style="color: #00ccff;">m1</span>

and

<span style="color: #ffcc00;">n2</span>=<span style="color: #00ccff;">m2</span>

and because

<span style="color: #ffcc00;">n1</span>=<span style="color: #00ccff;">m2</span>

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

<span style="color: #ffcc00;"><span style="color: #00ccff;">m1</span></span>=<span style="color: #00ccff;">m2</span>

.

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: $\displaystyle \text{AInv}=\frac{1}{{ad-bc}}\left( {\begin{array}{*{20}{c}} d & {-b} \\ {-c} & a \end{array}} \right)$

Substituting in the numbers: $\displaystyle \text{AInv=}\frac{1}{{\left( {\left( 1 \right)*\left( 4 \right)-\left( 2 \right)*\left( 3 \right)} \right)}}*\left( {\begin{array}{*{20}{c}} 4 & {-2} \\ {-3} & 1 \end{array}} \right)=-\frac{1}{2}\left( {\begin{array}{*{20}{c}} 4 & {-2} \\ {-3} & 1 \end{array}} \right)=\left( {\begin{array}{*{20}{c}} {-2} & 1 \\ {\frac{3}{2}} & {-\frac{1}{2}} \end{array}} \right)$ In Octave/MATLAB to find the Inv Matrix we can use the function

<span style="color: #3366ff;">inv( )</span>

:

AInv=<span style="color: #3366ff;">AInv(</span>A<span style="color: #3366ff;">)</span> 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:

1. B(1,1)+2*B(2,1)=19
2. B(1,2)+2*B(2,2)=22
3. 3*B(1,1)+4*B(2,1)=43
4. 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*<span style="color: #ff0000;">B*inv(B)</span>=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
<span style="color: #ff0000;">inv(A)*A</span>*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

This site uses Akismet to reduce spam. Learn how your comment data is processed.