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
<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
thus:
 Array Addition
<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)
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:
To write this in Octave/MATLAB we would use
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:
If these are instead written as a column vector we will have:
To write this in Octave/MATLAB we would use
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
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
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:
To write this in Octave/MATLAB we would use
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
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
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
To write this in Octave/MATLAB we would use
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
(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
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
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.
To write this in Octave/MATLAB we would use
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
(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
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.
For all element by element operations the Matrix Dimensions must match, i.e.
and
. If we take
The dimensions of the matrix after carrying element by element operations will also match:
i.e.
and
.
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:
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:
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:
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:
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:
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.
For array addition and array subtraction operations the Matrix Dimensions must match, i.e.
and
. If we take
The dimensions of the matrix after carrying element by element operations will also match:
The dimensions of the matrix after carrying element by element operations will also match:
i.e.
and
.
For array multiplication:
The inner dimensions of the two matrices being multiplied must match i.e.
.
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:
Array Exponentiation
For Array Exponentiation.
For array multiplication, to be valid i.e.
the following condition must be met
. Now for array exponentiation, the two arrays being multiplied are the same thus
and
and because
the only condition which satisfies the rule of matrix multiplication is that of a square matrix i.e.
.
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:
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
:
Now when a Matrix is multiplied by it's inverse will give the Identity matrix I:
If we multiply the expression
throughout by the inverse of A:
When a Matrix is multiplied by the Identity Matrix it is unchanged
Therefore:
This gives the correct value of B.
This can be thought of as:
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)=192*B(2,1)
Substituting into 3:
576*B(2,1)+4*B(2,1)=43
2*B(2,1)=4357
2*B(2,1)=14
B(2,1)=14/2
B(2,1)=7
Substituting this into 1 (which was rearranged):
Now rearranging 2:
Now substituting into 4:
Substituting this into the rearranged 2:
This gives:
As expected.
Octave/MATLAB can perform Matrix Division. Let's look at the case:
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:
It is useful to think of this in terms of the square matrices given above where
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:
It is likewise useful to think of this as
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: