Tutorial Video
VIDEO
The Colon Operator
Row=1<span style="color: #ff0000;">:</span>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=<span style="color: #ff6600;">[</span>1<span style="color: #ff0000;">:</span>10<span style="color: #ff6600;">]</span><span style="color: #ffcc00;">'</span>
By default the step size is 1, it however can be changed by specifying a step size in the middle
Row=1<span style="color: #ff0000;">:</span>2<span style="color: #ff0000;">:<span style="color: #000000;">9</span></span>
Row=1<span style="color: #ff0000;">:</span>2<span style="color: #ff0000;">:<span style="color: #000000;">10</span></span>
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:
<span style="color: #ff0000;">[</span>Output<span style="color: #ff0000;">]</span>=<span style="color: #0000ff;">linspace(</span>start<span style="color: #808080;">,</span>end<span style="color: #808080;">,</span>numberofsteps<span style="color: #0000ff;">)</span>
Row=<span style="color: #0000ff;">linspace(</span>1<span style="color: #808080;">,</span><span style="color: #ff0000;"><span style="color: #000000;">10<span style="color: #808080;">,</span>5<span style="color: #0000ff;">)</span></span></span>
Finding the Size of a Matrix: size function
The function size is useful for querying dimensions of a Matrix.
<span style="color: #ff0000;">[</span>NoofRows<span style="color: #808080;">,N</span>oofCols<span style="color: #ff0000;">]</span>=<span style="color: #0000ff;">size(</span>M<span style="color: #0000ff;">)</span>
M=<span style="color: #ff0000;">[</span>1<span style="color: #808080;">,</span>2<span style="color: #808080;">,</span>3<span style="color: #ff00ff;">;</span>4<span style="color: #808080;">,</span>5<span style="color: #808080;">,</span>6<span style="color: #ff0000;">]</span>
<span style="color: #ff0000;">[</span>m<span style="color: #808080;">,</span>n<span style="color: #ff0000;">]</span>=<span style="color: #0000ff;">size(</span>M<span style="color: #0000ff;">)</span>
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:
<span style="color: #ff0000;">[</span>M<span style="color: #ff0000;">]</span>=<span style="color: #0000ff;">reshape(</span>V<span style="color: #808080;">,</span><span style="color: #ff0000;">[</span>m<span style="color: #808080;">,</span>n<span style="color: #ff0000;">]</span><span style="color: #0000ff;">)</span>
This function reorders the Column Vector
by Column order to a Matrix with
Rows and
Columns.
Col=<span style="color: #ff9900;">[</span>1<span style="color: #ff0000;">:</span>10<span style="color: #ff9900;">]</span><span style="color: #ff6600;">'</span>
Then:
M=<span style="color: #0000ff;">reshape(</span>Col<span style="color: #808080;">,</span><span style="color: #ff0000;">[</span>5<span style="color: #808080;">,</span>2<span style="color: #ff0000;">]</span><span style="color: #0000ff;">)</span>
Recall when a colon was used as the solo input argument to index a Matrix M
<span style="color: #0000ff;">M(</span>:<span style="color: #0000ff;">)</span>
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
and
of the other input arguments.
M2=<span style="color: #0000ff;">reshape(</span>Col<span style="color: #808080;">,</span><span style="color: #ff0000;">[</span>2<span style="color: #808080;">,</span>5<span style="color: #ff0000;">]</span><span style="color: #0000ff;">)</span>
M3=<span style="color: #0000ff;">reshape(</span>Col<span style="color: #808080;">,</span><span style="color: #ff0000;">[</span>5<span style="color: #808080;">,</span><span style="color: #808080;">2</span><span style="color: #ff0000;">]</span><span style="color: #0000ff;">)<span style="color: #ff6600;">'</span></span>
<span style="color: #0000ff;"><span style="color: #ff6600;"><span style="color: #000000;">M3=M</span>'</span></span>
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=<span style="color: #0000ff;">zeros(</span>m<span style="color: #808080;">,</span>n<span style="color: #0000ff;">)</span>
where the input arguments
give the number of rows and
the number of columns respectively for the output zero matrix Zmn. For example:
Z25=<span style="color: #0000ff;">zeros(</span>2,5<span style="color: #0000ff;">)</span>
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=<span style="color: #0000ff;">ones(</span>m<span style="color: #808080;">,</span>n<span style="color: #0000ff;">)</span>
where the input arguments
give the number of rows and
the number of columns respectively for the output ones matrix Omn. For example:
O25=<span style="color: #0000ff;">ones(</span>2,5<span style="color: #0000ff;">)</span>
Of course a monochrome matrix can be made of any value simply by multiplication of a scalar. For instance:
T25=2*<span style="color: #0000ff;">ones(</span>2,5<span style="color: #0000ff;">)</span>
Generating Random Numbers: functions rand, randn and randi
There are three MATLAB functions for generating Random Numbers.
The function:
Random_0_1_mn=<span style="color: #0000ff;">rand(</span>m<span style="color: #808080;">,</span>n<span style="color: #0000ff;">)</span>
which generates random numbers between 0 and 1.
The function
Random_Normal_mn=<span style="color: #0000ff;">randn(</span>m<span style="color: #808080;">,</span>n<span style="color: #0000ff;">)</span>
which generates random numbers normally distributed about the origin.
Random_Integer_mn=<span style="color: #0000ff;">randi(</span>Imax<span style="color: #808080;">,</span>m<span style="color: #808080;">,</span>n<span style="color: #0000ff;">)</span>
which generates random integer numbers between 1 and
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:
<span style="color: #0000ff;">rng(</span><span style="color: #800080;">'</span>default<span style="color: #800080;">'</span><span style="color: #0000ff;">)</span>
If we alternatively want to shuffle it, so it is not predictable. We would instead use the command:
<span style="color: #0000ff;">rng(</span><span style="color: #800080;">'</span>shuffle<span style="color: #800080;">'</span><span style="color: #0000ff;">)</span>
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:
<span style="color: #0000ff;">rng(</span><span style="color: #800080;">'</span>default<span style="color: #800080;">'</span><span style="color: #0000ff;">)</span>
M=<span style="color: #0000ff;">rand(</span>2<span style="color: #808080;">,</span>5<span style="color: #0000ff;">)</span>
Create a Matrix that is 2 Rows by 5 Columns that is randomly is normally distributed, using the random number generator at default:
<span style="color: #0000ff;">rng(</span><span style="color: #800080;">'</span>default<span style="color: #800080;">'</span><span style="color: #0000ff;">)</span>
M=<span style="color: #0000ff;">randn(</span>2<span style="color: #808080;">,</span>5<span style="color: #0000ff;">)</span>
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:
<span style="color: #0000ff;">rng(</span><span style="color: #800080;">'</span>default<span style="color: #800080;">'</span><span style="color: #0000ff;">)</span>
M=<span style="color: #0000ff;">randi(</span>10<span style="color: #808080;">,</span>2<span style="color: #808080;">,</span>5<span style="color: #0000ff;">)</span>
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.
<span style="color: #0000ff;">rng(</span><span style="color: #800080;">'</span>default<span style="color: #800080;">'</span><span style="color: #0000ff;">)</span>
M=<span style="color: #0000ff;">randi(</span>10<span style="color: #ff0000;">+1</span><span style="color: #808080;">,</span>2<span style="color: #808080;">,</span>5<span style="color: #0000ff;">)<span style="color: #ff0000;">-1</span></span>
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:
<span style="color: #0000ff;">rng(</span><span style="color: #800080;">'</span>default<span style="color: #800080;">'</span><span style="color: #0000ff;">)</span>
M=<span style="color: #0000ff;">randi(<span style="color: #000000;">10<span style="color: #ff9900;">+10</span><span style="color: #ff0000;">+1</span><span style="color: #808080;">,</span></span></span>2<span style="color: #808080;">,</span>3<span style="color: #0000ff;">)<span style="color: #000000;"><span style="color: #ff9900;">-10</span><span style="color: #ff0000;">-1</span></span></span>
Diagonal: function diag
The function:
<span style="color: #ff0000;">[</span>M<span style="color: #ff0000;">]</span>=<span style="color: #0000ff;">diag(</span>V<span style="color: #0000ff;">)</span>
Will create a diagonal matrix if it's input is a vector.
V=<span style="color: #ff0000;">[</span>1<span style="color: #ff6600;">:</span>4<span style="color: #ff0000;">]</span><span style="color: #ff6600;">'</span>
M=<span style="color: #0000ff;">diag(</span>V<span style="color: #0000ff;">)</span>
If alternatively a Matrix is input, it will yield the values of the diagonal as a Column Vector:
<span style="color: #ff0000;">[</span>V2<span style="color: #ff0000;">]</span>=<span style="color: #0000ff;">diag(</span>M<span style="color: #0000ff;">)</span>
Identity Matrix: function eye
Multiplying a Vector or scalar by the Identity Matrix leaves it unchanged. Let's demonstrate this using the Matrix
M=<span style="color: #0000ff;">reshape(</span><span style="color: #ff0000;">[</span>1<span style="color: #ff6600;">:</span>9<span style="color: #ff0000;">]</span><span style="color: #ff6600;">'</span><span style="color: #808080;">,</span><span style="color: #ff0000;">[</span>3<span style="color: #808080;">,</span>3<span style="color: #ff0000;">]</span><span style="color: #0000ff;">)<span style="color: #ff0000;"><span style="color: #ff6600;">'</span></span></span>
All values in the Identity Matrix are equal to 0 unless they are on the main diagonal where they are equal to 1.
<span style="color: #ff0000;">[</span>I33<span style="color: #ff0000;">]</span>=<span style="color: #0000ff;">eye(</span>3<span style="color: #808080;">,</span>3<span style="color: #0000ff;">)</span>
Flipping a Matrix: functions fliplr and flipud
The function
<span style="color: #ff0000;">[</span>M_flipped_up_down<span style="color: #ff0000;">]</span>=<span style="color: #0000ff;">flipud(</span>M<span style="color: #0000ff;">)</span>
flips the input Matrix
up and down.
The function
<span style="color: #ff0000;">[</span>M_flipped_left_right<span style="color: #ff0000;">]</span>=<span style="color: #0000ff;">fliplr(</span>M<span style="color: #0000ff;">)</span>
flips the input Matrix
left and right.
M=<span style="color: #0000ff;">reshape(</span>1<span style="color: #ff0000;">:</span>16<span style="color: #808080;">,</span><span style="color: #ff0000;">[</span>4<span style="color: #808080;">,</span>4<span style="color: #ff0000;">]</span><span style="color: #0000ff;">)</span><span style="color: #ff6600;">'</span>
<span style="color: #ff0000;">[</span>M2<span style="color: #ff0000;">]</span>=<span style="color: #0000ff;">flipud(</span>M<span style="color: #0000ff;">)</span>
<span style="color: #ff0000;">[</span>M3<span style="color: #ff0000;">]</span>=<span style="color: #0000ff;">flipud(</span>M2<span style="color: #0000ff;">)</span>
<span style="color: #ff0000;">[</span>M4<span style="color: #ff0000;">]</span>=<span style="color: #0000ff;">fliplr(</span>M<span style="color: #0000ff;">)</span>
<span style="color: #ff0000;">[</span>M5<span style="color: #ff0000;">]</span>=<span style="color: #0000ff;">fliplr(</span>M4<span style="color: #0000ff;">)</span>
The Magic Square: function magic
The function magic creates a magic square:
M=<span style="color: #3366ff;">magic(</span>m<span style="color: #3366ff;">)</span>
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=<span style="color: #3366ff;">magic(</span>4<span style="color: #3366ff;">)</span>
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.
Like this: Like Loading...