Functions to Quickly Create Vectors and Matrices

Tutorial Video

The Colon Operator

Row=1: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=[1:10]'

\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:2:9

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

Row=1:2:10

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

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

Row=linspace(1,10,5)

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

[NoofRows,NoofCols]=size(M)

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

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

[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]'

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

Then:

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

\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

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.

\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=reshape(Col,[2,5])

\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=reshape(Col,[5,2])'

M3=M'

\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=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)

\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=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)

\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*ones(2,5)

\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=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)

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

rng('default')

M=randn(2,5)

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

rng('default')

M=randi(10,2,5)

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

rng('default')

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

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

rng('default')

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

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

[M]=diag(V)

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

V=[1:4]'

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

M=diag(V)

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

[V2]=diag(M)

\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=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)

\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

[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])'

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

[M2]=flipud(M)

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

[M3]=flipud(M2)

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

[M4]=fliplr(M)

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

[M5]=fliplr(M4)

\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=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)

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