# 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 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 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 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 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 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`

.