# Tutorial Video

# The Colon Operator

`Row=1:10`

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

If we want a column vector opposed to a Row Vector we can use the transpose:

`Col=[1:10]'`

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

`Row=1:2:9`

`Row=1:2:10`

Although we specified 10 as the last value, the last step in the sequence before 10 takes us to 9 and the next step is to 11. The sequence stops when we get to 10 and no higher number is recorded.

# Linear Spacing: linspace function

If we want both the start and the end point and we want a certain number of evenly spaced data points, we are better to use the Linear Space function:

`[Output]=linspace(start,end,numberofsteps)`

`Row=linspace(1,10,5)`

# Finding the Size of a Matrix: size function

The function size is useful for querying dimensions of a Matrix.

`[NoofRows,NoofCols]=size(M)`

`M=[1,2,3;4,5,6]`

`[m,n]=size(M)`

`m=2`

`n=3`

# Reshaping an Array: reshape function

Supposing we want to create a matrix C which has 2 columns the first going from 1 to 5 and the second going from 6 to 10. We could setÂ A=[1:5]’ and B=[6:10]’ and then concatenate A and B using C=[A,B]. However if we have a larger matrix, we could save some time using:

`[M]=reshape(V,[m,n])`

This function reorders the Column Vector `V`

Â by Column order to a Matrix with `m`

Rows and `n`

Columns.

`Col=[1:10]'`

Then:

`M=reshape(Col,[5,2])`

Recall when a colon was used as the solo input argument to index aÂ Matrix M

`M(:)`

It looses it’s shape and all the elements are read out in column order meaning we get the first column, at the bottom of that, the second column is added, at the bottom of that the third column is added and so on and so forth.

Reshape similarly works in column order but then creates a new matrix using the specified `m`

and `n`

of the other input arguments.

`M2=reshape(Col,[2,5])`

`M3=reshape(Col,[5,2])'`

`M3=M'`

# A Matrix of zeros: function zeros

One useful function is zeros which allows one to make a matrix full of zeros. Why you ever want to do that? Take for example the screen of the XPS 13 9365, to conserve battery power you dim the screen when the system is sleeping.

How would you do that? You would do that by sending zeros to every pixel on the screen. The function zeros has the form

`Zmn=zeros(m,n)`

where the input arguments `m`

give the number of rows and `n`

the number of columns respectively for the output zero matrix Zmn. For example:

`Z25=zeros(2,5)`

# A Matrix of ones: function ones

A similar matrix is the ones matrix which allows one to make a matrix full of ones. The function ones has the form

`Omn=ones(m,n)`

where the input arguments `m`

give the number of rows and `n`

the number of columns respectively for the output ones matrix Omn. For example:

`O25=ones(2,5)`

Of course a monochrome matrix can be made of any value simply by multiplication of a scalar. For instance:

`T25=2*ones(2,5)`

# Generating Random Numbers: functions rand, randn and randi

There are three MATLAB functions for generating Random Numbers.

The function:

`Random_0_1_mn=rand(m,n)`

which generates random numbers between 0 and 1.

The function

`Random_Normal_mn=randn(m,n)`

which generates random numbers normally distributed about the origin.

`Random_Integer_mn=randi(Imax,m,n)`

which generates random integer numbers between 1 andÂ `Imax`

Â which is the first input of the function corresponding to the upper limit.

These three random number generators use a shared random number generator which defaults back to the beginning at startup. We can reset it any time we want using the command:

`rng('default')`

If we alternatively want to shuffle it, so it is not predictable. We would instead use the command:

`rng('shuffle')`

Create a Matrix that is 2 Rows by 3 Columns that is randomly distributed between 0 and 1, using the random number generator at default:

`rng('default')`

`M=rand(2,5)`

Create a Matrix that is 2 Rows by 5 Columns that is randomly is normally distributed, using the random number generator at default:

`rng('default')`

`M=randn(2,5)`

Create a Matrix that is 2 Rows by 5 Columns and consists of random integers between 1 and 10, using the random number generator at default:

`rng('default')`

`M=randi(10,2,5)`

We can use these as the building blocks for other scenarios. For instance create a Matrix that is 2 Rows by 5 Columns and consists of random integers between 0 and 10, using the random number generator at default.

`rng('default')`

`M=randi(10+1,2,5)-1`

For instanceÂ create a Matrix that is 2 Rows by 5 Columns and consists of random integers between -10 and 10, using the random number generator at default:

`rng('default')`

`M=randi(10+10+1,2,3)-10-1`

# Diagonal: function diag

The function:

`[M]=diag(V)`

Will create a diagonal matrix if it’s input is a vector.

`V=[1:4]'`

`M=diag(V)`

If alternatively a Matrix is input, it will yield the values of the diagonal as a Column Vector:

`[V2]=diag(M)`

# Identity Matrix: function eye

Multiplying a Vector or scalar by the Identity Matrix leaves it unchanged. Let’s demonstrate this using the Matrix

`M=reshape([1:9]',[3,3])'`

All values in the Identity Matrix are equal to 0 unless they are on the main diagonal where they are equal to 1.

`[I33]=eye(3,3)`

# Flipping a Matrix: functions fliplr and flipud

The function

`[M_flipped_up_down]=flipud(M)`

flips the input Matrix `M`

up and down.

The function

`[M_flipped_left_right]=fliplr(M)`

flips the input Matrix `M`

left and right.

`M=reshape(1:16,[4,4])'`

`[M2]=flipud(M)`

`[M3]=flipud(M2)`

`[M4]=fliplr(M)`

`[M5]=fliplr(M4)`

# The Magic Square: function magic

The function magic creates a magic square:

`M=magic(m)`

Since it is a square we only need one input argument. Note if one input argument is input for the function zeros, ones, eye, rand, randn or two inputs selected for randi then it is likewise assumed to be square i.e. have dimensionsÂ `m=n`

`M=magic(4)`

The magic square is a matrix where every row, column, diagonal and anti-diagonal match up to an equal number in this case 34.

Use logical indexing and the sum function to go through each row to make sure they add up to 34, repeat for the columns making sure they add up to 34. Next check the diagonal adds up to 34 using the sum and diag functions. Check the antidiagonal adds up to 34, use the fliplr, sum and diag functions.