Functions to Quickly Create Vectors and Matrices

Tutorial Video

The Colon Operator

Row=1<span style="color: #ff0000;">:</span>10

\displaystyle \text{Row}=\left[ {\begin{array}{*{20}{c}} 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & {10} \end{array}} \right]

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>

\displaystyle \text{Col}=\left[ {\begin{array}{*{20}{c}} 1 \\ 2 \\ 3 \\ 4 \\ 5 \\ 6 \\ 7 \\ 8 \\ 9 \\ {10} \end{array}} \right]

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>

\displaystyle \text{Row}=\left[ {\begin{array}{*{20}{c}} 1 & 3 & 5 & 7 & 9 \end{array}} \right]

Row=1<span style="color: #ff0000;">:</span>2<span style="color: #ff0000;">:<span style="color: #000000;">10</span></span>

\displaystyle \text{Row}=\left[ {\begin{array}{*{20}{c}} 1 & 3 & 5 & 7 & 9 \end{array}} \right]

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>

\displaystyle \text{Row}=\left[ {\begin{array}{*{20}{c}} 1 & {3.25} & {5.5} & {7.75} & {10} \end{array}} \right]


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>

\displaystyle \text{M}=\left[ {\begin{array}{*{20}{c}} 1 & 2 & 3 \\ 4 & 5 & 6 \end{array}} \right]

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

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:

<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

V

 by Column order to a Matrix with

m

Rows and

n

Columns.

Col=<span style="color: #ff9900;">[</span>1<span style="color: #ff0000;">:</span>10<span style="color: #ff9900;">]</span><span style="color: #ff6600;">'</span>

\displaystyle \text{Col}=\left[ {\begin{array}{*{20}{c}} 1 \\ 2 \\ 3 \\ 4 \\ 5 \\ 6 \\ 7 \\ 8 \\ 9 \\ {10} \end{array}} \right]

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>

\displaystyle \text{M}=\text{reshape}\left( {\text{Col,}\left[ {5,2} \right]} \right)=\left[ {\begin{array}{*{20}{c}} 1 & 6 \\ 2 & 7 \\ 3 & 8 \\ 4 & 9 \\ 5 & {10} \end{array}} \right]

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.

\displaystyle \text{M(:)}=\left[ {\begin{array}{*{20}{c}} 1 \\ 2 \\ 3 \\ 4 \\ 5 \\ 6 \\ 7 \\ 8 \\ 9 \\ {10} \end{array}} \right]

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

m

and

n

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>

\displaystyle \text{M2}=\text{reshape}\left( {\text{M,}\left[ {2,5} \right]} \right)=\left[ {\begin{array}{*{20}{c}} 1 & 3 & 5 & 7 & 9 \\ 2 & 4 & 6 & 8 & {10} \end{array}} \right]

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>

\displaystyle \text{M3}=\text{reshape}\left( {\text{M,}\left[ {5,2} \right]} \right)'=\text{M}'=\left[ {\begin{array}{*{20}{c}} 1 & 2 & 3 & 4 & 5 \\ 6 & 7 & 8 & 9 & {10} \end{array}} \right]


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

m

give the number of rows and

n

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>

\displaystyle \text{Z25}=\text{zeros(2,5)}=\left[ {\begin{array}{*{20}{c}} 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 \end{array}} \right]


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

m

give the number of rows and

n

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>

\displaystyle \text{O25}=\text{zeros(2,5)}=\left[ {\begin{array}{*{20}{c}} 1 & 1 & 1 & 1 & 1 \\ 1 & 1 & 1 & 1 & 1 \end{array}} \right]

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>

\displaystyle \text{T25}=2*\text{ones(2,5)}=\left[ {\begin{array}{*{20}{c}} 2 & 2 & 2 & 2 & 2 \\ 2 & 2 & 2 & 2 & 2 \end{array}} \right]


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 

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:

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

\displaystyle \text{rand(2,5)}=\left[ {\begin{array}{*{20}{c}} {0.8147} & {0.1270} & {0.6324} & {0.2785} & {0.9575} \\ {0.9058} & {0.9134} & {0.0975} & {0.5469} & {0.9649} \end{array}} \right]

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>

\displaystyle \text{randn(2,5)}=\left[ {\begin{array}{*{20}{c}} {0.5377} & {-2.2588} & {0.3188} & {-0.4336} & {3.5784} \\ {1.8339} & {0.8622} & {-1.3077} & {0.3426} & {2.7694} \end{array}} \right]

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>

\displaystyle \text{M}=\text{randi(10,2,5)}=\left[ {\begin{array}{*{20}{c}} 9 & 2 & 7 & 3 & {10} \\ {10} & {10} & 1 & 6 & {10} \end{array}} \right]

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>

\displaystyle \text{M}=\text{randi}(10+1,2,5)-1=\left[ {\begin{array}{*{20}{c}} 8 & 1 & 6 & 3 & {10} \\ 9 & {10} & 1 & 6 & {10} \end{array}} \right]

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>

\displaystyle \text{M}=\text{randi}(10+10+1,2,5)-1-10=\left[ {\begin{array}{*{20}{c}} 7 & {-8} & 3 & {-5} & {10} \\ 9 & 9 & {-8} & 1 & {10} \end{array}} \right]


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>

\displaystyle \text{V}=[1:4]'=\left[ {\begin{array}{*{20}{c}} 1 \\ 2 \\ 3 \\ 4 \end{array}} \right]

M=<span style="color: #0000ff;">diag(</span>V<span style="color: #0000ff;">)</span>

\displaystyle \text{M}=\text{diag(V)}=\left[ {\begin{array}{*{20}{c}} 1 & 0 & 0 & 0 \\ 0 & 2 & 0 & 0 \\ 0 & 0 & 3 & 0 \\ 0 & 0 & 0 & 4 \end{array}} \right]

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>

\displaystyle \text{V2}=\text{diag(M)}=\left[ {\begin{array}{*{20}{c}} 1 \\ 2 \\ 3 \\ 4 \end{array}} \right]


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>

\displaystyle \text{I33}=\text{eyes(3,3)}=\left[ {\begin{array}{*{20}{c}} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{array}} \right]

\displaystyle \text{M}*\text{I33}=\left[ {\begin{array}{*{20}{c}} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{array}} \right]*\left[ {\begin{array}{*{20}{c}} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{array}} \right]=\left[ {\begin{array}{*{20}{c}} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{array}} \right]=\text{M}

\displaystyle \text{I33}*\text{M}=\left[ {\begin{array}{*{20}{c}} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{array}} \right]*\left[ {\begin{array}{*{20}{c}} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{array}} \right]=\left[ {\begin{array}{*{20}{c}} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{array}} \right]=\text{M}


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

M

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

M

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>

\displaystyle \text{M}=\left[ {\begin{array}{*{20}{c}} 1 & 2 & 3 & 4 \\ 5 & 6 & 7 & 8 \\ 9 & {10} & {11} & {12} \\ {13} & {14} & {15} & {16} \end{array}} \right]

<span style="color: #ff0000;">[</span>M2<span style="color: #ff0000;">]</span>=<span style="color: #0000ff;">flipud(</span>M<span style="color: #0000ff;">)</span>

\displaystyle \text{M2}=\text{flipud(M)}=\left[ {\begin{array}{*{20}{c}} {13} & {14} & {15} & {16} \\ 9 & {10} & {11} & {12} \\ 5 & 6 & 7 & 8 \\ 1 & 2 & 3 & 4 \end{array}} \right]

<span style="color: #ff0000;">[</span>M3<span style="color: #ff0000;">]</span>=<span style="color: #0000ff;">flipud(</span>M2<span style="color: #0000ff;">)</span>

\displaystyle \text{M3}=\text{flipud(M)}=\left[ {\begin{array}{*{20}{c}} 1 & 2 & 3 & 4 \\ 5 & 6 & 7 & 8 \\ 9 & {10} & {11} & {12} \\ {13} & {14} & {15} & {16} \end{array}} \right]=\text{M}

<span style="color: #ff0000;">[</span>M4<span style="color: #ff0000;">]</span>=<span style="color: #0000ff;">fliplr(</span>M<span style="color: #0000ff;">)</span>

\displaystyle \text{M4}=\text{fliplr(M)}=\left[ {\begin{array}{*{20}{c}} 4 & 3 & 2 & 1 \\ 8 & 7 & 6 & 5 \\ {12} & {11} & {10} & 9 \\ {16} & {15} & {14} & {13} \end{array}} \right]

<span style="color: #ff0000;">[</span>M5<span style="color: #ff0000;">]</span>=<span style="color: #0000ff;">fliplr(</span>M4<span style="color: #0000ff;">)</span>

\displaystyle \text{M5}=\text{fliplr(M)}=\left[ {\begin{array}{*{20}{c}} 1 & 2 & 3 & 4 \\ 5 & 6 & 7 & 8 \\ 9 & {10} & {11} & {12} \\ {13} & {14} & {15} & {16} \end{array}} \right]=\text{M}

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=n
M=<span style="color: #3366ff;">magic(</span>4<span style="color: #3366ff;">)</span>

\displaystyle \text{M}=\left[ {\begin{array}{*{20}{c}} {16} & 2 & 3 & {13} \\ 5 & {11} & {10} & 8 \\ 9 & 7 & 6 & {12} \\ 4 & {14} & {15} & 1 \end{array}} \right]

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.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.