## Tutorial Video

## Recap

If you are unsure of the terminology, scalar, vectors, column, row, matrix and page take a look at MATLAB: Visualising Scalars, Vectors, Matrices and Pages. We will look at inputting these in MATLAB.

## Creating a 1D Scalar in MATLAB

We can assign a scalar with a Variable Name `Scalar`

to a value `1`

using:

This will give the result:

`Scalar=1`

In this case Scalar is a 1 by 1 Array (a 1 by 1 Array has the special name Scalar):

We can open it up in the Variable Editor to see it in as a Spreadsheet:

To input more complicated items such as a vector or matrix, all terms contained within the array must be enclosed in square brackets.

This gives the same result for the Scalar:

`Scalar=1`

## Creating a Row Vector in MATLAB

A Row Vector is an array that has one row and multiple columns. When inputting a row vector in MATLAB we need to use a delimiter to separate out the numeric values in each column of the row. These are enclosed in `[ ]`

and in MATLAB to move onto the next Column → we use the comma `,`

as a delimiter.

This gives:

Note in the Variable Editor this is listed as a 1×3 meaning 1 Row by 3 Columns.

## Creating a Column Vector in MATLAB

A Column Vector is an array that has multiple rows and one column. When inputting a column vector in MATLAB we need to use a delimiter to separate out the numeric values in each row of the column. These are enclosed in `[ ]`

and in MATLAB to move onto the next Column ↓ we use the comma `;`

as a delimiter.

This gives:

Note in the Variable Editor this is listed as a 4×1 meaning 4 Rows by 1 Column.

## Creating a Matrix

A Matrix is an array that has multiple rows and multiple columns. For this we will need to use both the `,`

Delimiter (instructions to move →) and `;`

Delimiter (instructions to move ↓) respectively. The convention is to use the `,`

Delimiters first to construct the 1st Row Vector and then the first `;`

Delimiter to move onto the 2nd Row where we can once again use the `,`

Delimiters first to construct the 2nd Row Vector and then the second `;`

Delimiter to move onto the 3rd Row where we can once again use the `,`

Delimiters first to construct the 3rd Row Vector and so on and so forth.

If we are using the Command Window, we can type the following on one line:

If we are using a script (discussed in another guide) we can split it over multiple lines using:

This gives:

Note in the Variable Editor this is listed as a 4×4 meaning 4 Rows by 4 Columns.

## Delimiters , vs ;

Note when input Matrices it may help if you conceptualise the matrix as a series of rows opposed to a series of columns.

If one attempts to input them this way

The following error is flagged up:

Dimensions of arrays being concatenated are not consistent.

This is because the `,`

moves only → to the next column

whereas the `;`

moves to to the first column and ↓ and to the next row

Matlab therefore reads the following code as:

This explains why MATLAB flags up the error:

Dimensions of arrays being concatenated are not consistent.

Because all the cells with the ? are unspecified.

The above code can be amended using additional `[ ]`

brackets. Here each column will be created and then concatenated side by side together to form the Matrix:

We can also created the four columns as separate variables and then concatenate them together:

We could also create the four rows and concatenate them together:

The following would also work:

## Logical Indexing of a Single Matrix Element

Supposing with have the Matrix Mat and we want to select individual elements from the Mat highlighted in sky blue, red, yellow, magenta, purple and green respectively, we can use logically indexing to do this:

We can look up an element in a Matrix by typing the Variable Name of the Matrix followed by circular brackets with two input arguments, the first input argument is the Row and the second one is the Column. This is similar to functions which we will look at in more depth later:

Or because we are only outputting one Variable Name we can use:

Let's read the Sky Blue Element from the Matrix Mat, we see that it is on the 1st row and the 4th column.

We get the value 4 as expected

`SkyBlueElement=4`

Let's read the Red Element from the Matrix Mat, we see that it is on the 2nd row and the 2nd column.

`RedElement=6`

Let's read the Yellow Element from the Matrix Mat, we see that it is on the 3rd row and the 3rd column.

`YellowElement=11`

Let's read the Magenta Element from the Matrix Mat, we see that it is on the 4th row and the 4th column.

`MagentaElement=16`

Let's read the Purple Element from the Matrix Mat, we see that it is on the 4th row and the 2nd column.

`PurpleElement=14`

Let's read the Green Element from the Matrix Mat, we see that it is on the 1st row and the 1st column.

`GreenElement=1`

## Colon Operator

So far we have shown a very small Row vector:

It is pretty easy to type out in full but there is a shortcut we can use in form of the colon operator `:`

In this case, we don't need to enclose this in Square Brackets:

However if we were to use the Colon operator alongside a comma, for instance

Failure to include them:

i.e. gives two output variables but only the first is assigned so the second is output as `ans`

. To output this as two Variables we could use:

Although it useful to sometimes include the Square Brackets to see more clearly what is going on.

For learning purposes we are just going to go up to 10, so you can see what's going on. When you are grounded in the fundamentals you can then go an tackle matrices that are larger.

We start at 10 and move in steps of 1 until we get to 10.

With this the maximum number was set to 9.81 and the starting number was 1. The closest number that is 1 plus multiple steps of 1 to 9.81 that is less than 9.81 is 9 so we are given 9 as the maximum value.

By default the step size is 1, it however can be changed by specifying a step size in the middle:

We do not need to specify an integer as the step:

## Transposing

A row vector can be transposed into a column vector and a row vector can be transposed into a column vector. To transpose in MATLAB we use `'`

Matrices themselves can also be transposed. lets create a Matrix `M`

and transpose it:

In the transpose each element of the matrix `M(a,b)`

becomes `Mt(b,a)`

. For instance the element of the matrix at `M(3,2)`

which is 12 re-positions at `Mt(2,3)`

in the transpose matrix.

## Array Transpose

A Matrix of numbers is a 2D array. If instead of regular numbers complex numbers are used, these numbers have a real and imaginary part. The real part lies on the xy plane and the complex part lies on the z axis. Conceptually transposing should be thought of as flipping the axes around the matrix in 3D space:

Before the transpose the x values are shown as the blue arrow and the y values as the red arrow. The third axis z axis is the green arrow and is imaginary. If you look at the screen and take your right hand and stretch it out to your right hand side and point, and assign your index finger to the blue arrow, next stretch your middle finger out and thumb so they are at right angles to each other. Rotate our arm counterclockwise so you are still pointing to the right, but your middle finger is pointing down, assign this finger to the red arrow. You should see that your thumb not points in the way towards the screen. If you now keep your fingers in that position and bend your arm so your index finger now points towards the ground, and your middle finger points to your right, you'll see your thumb now points out the way away from the screen. In other words you have transposed your index finger and middle finger and your thumb points in the negative direction.

Thus when transposing a matrix which contains complex numbers, the complex component will switch sign. To prevent this array transposing `.'`

can be used instead (this will be discussed in more detail later on).

## The Colon Operator and Transposing

To create a Column vector quickly we can create a Row Vector and Transpose it. We can combine this in a single line.

Note we need to use the square brackets otherwise we will transpose the scalar 8 which transposes to itself and end up with the Row Vector.

## Suppressing Output

This is particularly useful if we want to look at all the rows of some hardware for example the XPS 13 9365 which has a row of 3600 pixels and column of 1800 pixels. We would be all day trying to type the 3600 and 1800 numbers respectively…

We can get:

MATLAB can calculate large arrays such as the above relatively quickly. However outputting all these to the Command Window will substantially delay the code as there is a delay so the user has a chance to read it. It is not necessary for the user to read out 3200 and 1800 sequential numbers for instance so we can suppress the output.

If we don't want to fill our Command Window with all these values we can end the statement in a `;`

to suppress the output. In this case we are not using the `;`

as a Delimiter as it is not present within the `[ ]`

but instead at the end of the statement.

## Logical Indexing

Supposing we want to use logical indexing to make sub-selections of the Matrix Mat

The Red Selection is the top left hand corner of the matrix Mat. A selection of the first two rows `[1;2]`

(input 1 is a column vector and selects the desired rows) and first two columns `[1,2]`

(input 2 is a row vector and selects the desired columns):

As MATLAB knows the first input for logical indexing has to be a column vector and the second input has to be a row vector, it will automatically transpose these. Thus the following three lines of code are all equivalent to the above.

The Blue Selection is the Last Column.

We can select it using the following:

However we know that 4 is the end value of the matrix, so we can modify the code above to:

We also know that it doesn't matter if we specify the input 1 as a Column Vector so we can drop the `'`

Since we are select all Rows another shortcut is to use a `:`

which on its own represents all elements.

Because we are specifying all values and just the end value for inputs 1 and input 2 respectively, we don't need to enclose these in the `[ ]`

thus the above can be simplified to:

The Yellow Selection is of the first 2 rows of the 2nd Last Column:

Since it is the Second Last Column we can also refer to it as the `end-1`

column.

We don't need the Second set of square brackets and we can use a `,`

instead of a ` ;`

And finally the Green Selection which is the 3rd and 4th Rows of the 1st three Columns:

This can also be defined from the end points:

Suppose we want to combine the fragments to create the following:

Conceptually we need to bring the `YellowSelection`

and `RedSelection`

together:

Then we can bring the `GreenSelection`

and `YellowRedSelection`

together:

Finally we can bring the `BlueSelection`

and `GreenYellowRedSelection`

together:

This can also be done in one line:

## Creating a 3D Array

We can visualise a 3D Array in Excel. We can see that it consists of three Pages and each Page is a 2 by 2 Matrix.

We can get started by Creating the Matrices corresponding to the individual pages:

Once the pages are made, logical indexing can be made this time when assigning the variable name. The first and second inputs to the variable name will be `:`

and `:`

as all rows and all columns are selected. The third input will be the page number which will be assigned to 1, 2 and 3 respectively.

The first line of code will just duplicate Page 1:

Adding the second page will create a 2 Rows by 2 Columns by 2 Pages Book:

Adding the third page will create the a 2 Rows by 2 Columns by 3 Pages Book as desired: