Introduction to Arrays in Octave and MATLAB

 

This a guide which takes you through the basics of arrays including scalars, vectors, matrices. It discusses concepts such as columns and rows in great detail and then moves onto how arrays are input into Octave/MATLAB.

Contents

  • Arrays: A Discussion about different types of Arrays
  • Creating Arrays in Octave/MATLAB using a Script or Command Window
  • Checking the size of an Array
  • Logical Indexing and Concatenation of Arrays
  • Transposing
  • The Colon Operator
  • Short Hand Logical Indexing
  • Linear Spacing
  • Reshape
  • Zeros
  • Ones
  • Creating a 3D Array

Arrays

Let's just use an excel spreadsheet with no numbers on it to give an overview of the different types of arrays. Once we understand these Arrays we can go ahead and look at inputting them into Octave/MATLAB and looking at some of their properties.

Scalar – 1 by 1 Array

In other words a scalar is just a single number.

Column Vector – m by 1 Array

A Column Vector can be considered as a list of scalars, listed in a column. These can be a list of times, or a list of distances, a list of speeds or a list of prices.

Row Vector – 1 by n array

A Row Vector can be considered as a list of scalars, listed in a row. This information can just be the same as presented in the Column Vector but in a different orientation. The best orientation depends on how the data can be manipulated later on, which will be discussed later on however Row Vectors can always be transposed into Column Vectors and vice versa.

Transposing

Transposing switches Rows and Columns and vice versa. This allows one to convert a Column Vector into a Row Vector and vice versa.

Matrix – m by n Array

A matrix can be thought of as a rectangular grid of scalars.

A matrix can be considered as the building up of a row by use of equally sized column vectors:

Taking the example of using a column vector as a list of prices of goods in a shop. The first column could be Asda, the second Tescos, the thirds Morrisons, the fourth Lidl and the fifth Aldi's… for instance.

Alternatively it can be considered as the building up of a column by use of equally sized row vectors:

An example of a Matrix is also the screen of your computer or phone or the sense of the imaging CCD in your camera. It is a rectangular object with many pixels and you can think of the pixel as a square in the excel spreadsheet. My Dell XPS 13 9365 has 3200 by 1800 of these squares. For a black and white image to display each pixel has to give off a specified brightness. For 8 Bit there are 256 (0-255) levels of grey.

3D Array m by n by p Array

This may look more complicated but think of this like flicking through pages in a book, each page has the same dimensions in terms of m and n but p (the page) has changed. In this case, the sheets of the spreadsheet.

A colour image is an example of a 3D Array. Instead of there being a single grey channel, there are now three colour channels corresponding to the three primary colours red, green and blue. For 8 bit there are 256 levels (0-255). Any other colour can be made by mixing different values of these colours.

Labelling Cells of a Matrix

Here the black line represents the boundary of the Matrix. By convention, we say the matrix has m rows by n columns often abbreviated a m by n matrix. In this case it is a matrix with 14 rows by 10 columns, known as a 14 by 10 matrix. Do not get the labelling mixed up when it comes to rows and columns. If it helps, think of entering a block of flats, which has a 3 floors and each floor has 4 apartment. You are going to find the floor before you look for the room number. The matrix element that we have highlighted is positioned at row 4 and column 2 (we've looking for apartment number 4.2 and have first climbed to the 4th flight then looked for apartment 2).

This is an example of a 4 by 8 matrix and the highlighted cell is at position 3,7.


Creating an Array in a Script File or via the Command Window

Previously we assigned the value of 1, to a variable name of x using the command:

x=1

\displaystyle x=1

In this case x is a 1 by 1 array otherwise known as a scalar.

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

x=[1]

\displaystyle x=[1]

Previously we seen when using the nthroot function that it had the form:

y=nthroot(x,n)

The two input arguments were separated by a comma , . For a row vector we also use the , to jump → over to the next cell.

Thus to assign the above to the variable row we type in:

row=[1,2,3]

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

If instead of jumping right, we want to jump ↓ we can use the semicolon ;

Thus to assign the above to the variable col we type in:

col=[2;4;6;8]

\displaystyle \text{col}=\left[ {\begin{array}{*{20}{c}} 2 \\ 4 \\ 6 \\ 8 \end{array}} \right]

To make a Matrix that is 4 by 4 like above we need to use both commas , and semicolons ; Note the comma . moves onto the next column while the semi-colon ; moves onto the start of the next row.

Mat=[1,2,3,4;5,6,7,8;9,10,11,12;13,14,15,16]

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

For convenience, in a script file this can also be written over multiple lines.

Mat=[1,2,3,4;
5,6,7,8;
9,10,11,12;
13,14,15,16]

[code language="matlab"]

Mat=[1,2,3,4;
5,6,7,8;
9,10,11,12;
13,14,15,16]

[/code]

 


Size of an Array

Although we can readily see the size of these matrices and read them off the variable editor, it is sometimes useful to read off the size of an array in Octave/MATLAB so this information can be used in more complex code. We can use the function size to get the dimensions of an array. The input argument x should be the array under investigation. The output arguments should be in square brackets [ ] and separated by a comma , i.e. of the same nature as a row vector.

[m,n]=size(x)

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

\displaystyle \text{col}=\left[ {\begin{array}{*{20}{c}} 2 \\ 4 \\ 6 \\ 8 \end{array}} \right]

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

[m,n]=size(row)

\displaystyle \begin{array}{l}m=1\\n=3\end{array}

[m,n]=size(col)

\displaystyle \begin{array}{l}m=4\\n=1\end{array}

[m,n]=size(Mat)

\displaystyle \begin{array}{l}m=4\\n=4\end{array}

Note the sizes of arrays are also shown in the workspace.


Logical Indexing

Logical indexing is extremely useful, if one wants to select only part of a Matrix, e.g. if one has a Matrix loaded up and wants to extract the first column of it, in order to plot it (I will make another guide on plotting). Here we will look at selecting individual Matrix elements and then vectors and columns and submatrices from a larger matrix. We also use concatenation to join smaller matrices or vectors together to make a larger matrix. Previously we seen when using the nthroot function that it had the form:

y=nthroot(x,n)

The two input arguments were separated by a comma , and enclosed by round brackets ( ). The output argument could be specified, otherwise it would just be printed out in the Command Window. Logical indexing of matrices takes on a similar form, Element=Mat(x,y) with the matrix acting in a similar manner to the function and the input arguments x and y selecting the row and column respectively, the output argument Element gives the value of the element of the Matrix. For instance we can look up the 2nd row and 2nd column of the Matrix Mat and assign it a variable name Mat22:

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

Mat22=Mat(2,2)

\displaystyle \text{Mat22}=6

Suppose we want to look up the matrix elements that are highlighted red, yellow, magenta, purple, sky blue and green.

Mat22=Mat(2,2)

\displaystyle \text{Mat22}=6

Mat33=Mat(3,3)

\displaystyle \text{Mat33}=11

Mat44=Mat(4,4)

\displaystyle \text{Mat44}=16

Mat42=Mat(4,2)

\displaystyle \text{Mat42}=14

Mat14=Mat(1,4)

\displaystyle \text{Mat14}=4

Mat11=Mat(1,1)

\displaystyle \text{Mat11}=1

So far for logical indexing using Element=Mat(x,y) we have used scalar input arguments x and y to select the row and column respectively. We can instead use vectors as input arguments and correspondingly create output arguments which contain multiple elements i.e. the output arguments are subselections that are themselves vectors or submatrices.

Use logical indexing to select the following parts of Mat, highlighted in red, yellow, green and blue.

For Mat_r we are looking for column 1 and 2 and row 1 and 2. Thus we have the first input argument as [1;2] and the second input argument as [1,2]

Mat_r=Mat([1;2],[1,2])

\displaystyle \text{Mat }\!\!\_\!\!\text{ r}=\left[ {\begin{array}{*{20}{c}} 1 & 2 \\ 5 & 6 \end{array}} \right]

For Mat_y we are looking for column 1 and 2 and row 3. Thus we have the first input argument as [1;2] and the second input argument as [3]

Mat_y=Mat([1;2],[3])=Mat([1;2],3)

\displaystyle \text{Mat }\!\!\_\!\!\text{ y}=\left[ {\begin{array}{*{20}{c}} 3 \\ 7 \end{array}} \right]

For Mat_g we are looking for column 3 and 4 and row 1,2 and 3. Thus we have the first input argument as [3;4] and the second input argument as [1,2,3]

Mat_g=Mat([3;4],[1,2,3])

\displaystyle \text{Mat }\!\!\_\!\!\text{ g}=\left[ {\begin{array}{*{20}{c}} 9 & {10} & {11} \\ {13} & {14} & {15} \end{array}} \right]

For Mat_b we are looking for column 1,2,3 and 4 and row 4. Thus we have the first input argument as [1;2;3;4] and the second input argument as [4]

Mat_b=Mat([1;2;3;4],[4])=Mat([1;2;3;4],4)

\displaystyle \text{Mat }\!\!\_\!\!\text{ b}=\left[ {\begin{array}{*{20}{c}} 4 \\ 8 \\ {12} \\ {16} \end{array}} \right]

For logical indexing

  • we treat the Matrix( ) as a function
  • input arguments are separated by a comma ,
  • the first input argument selects the row(s)
  • the second input argument selects the column(s)
  • the output argument is the matrix element(s)
  • the input arguments may be scalars in which case square brackets are optional
  • the input arguments may be vectors which require the [ ] brackets
  • if using vector input arguments the first input argument is a column vector and the second input argument is a row vector*

With the code above, I have strictly input a column vector as the first input argument and a row vector as the second input argument as it consistent with, the code used to create the Matrices above and it's best to be consistent for learning concepts. When using vector input arguments in logical indexing or in functions, Octave/MATLAB will often automatically convert a row vector into a column vector or a column vector into a row vector where needed. Octave/MATLAB document often favours the comma . to the semi-colon ; and initially when learning, this confused me. The following three lines of code are treated as the same:

Mat_g=Mat([3;4],[1,2,3])

Mat_g=Mat([3,4],[1,2,3])

Mat_g=Mat([3,4],[1;2;3])

Mat_g=Mat([3;4],[1;2;3])

Matrices can be built up using pre-existing variables opposed to typing every single number into the Command Window every time. However in order to concatenate rows (join rows together from two different variables) they must have the same number of rows m. Likewise in order to concatenate columns together (join columns together), they must have the same number of columns, n. Lets look at the size of the fragments we obtained from Mat

\displaystyle \text{Mat }\!\!\_\!\!\text{ r}=\left[ {\begin{array}{*{20}{c}} 1 & 2 \\ 5 & 6 \end{array}} \right]

[m,n]=size(Mat_r)

\displaystyle \begin{array}{l}\text{m}=2\\\text{n}=2\end{array}

\displaystyle \text{Mat }\!\!\_\!\!\text{ y}=\left[ {\begin{array}{*{20}{c}} 3 \\ 7 \end{array}} \right]

[m,n]=size(Mat_y)

\displaystyle \begin{array}{l}\text{m}=2\\\text{n}=1\end{array}

\displaystyle \text{Mat }\!\!\_\!\!\text{ g}=\left[ {\begin{array}{*{20}{c}} 9 & {10} & {11} \\ {13} & {14} & {15} \end{array}} \right]

[m,n]=size(Mat_g)

\displaystyle \begin{array}{l}\text{m}=2\\\text{n}=3\end{array}

\displaystyle \text{Mat }\!\!\_\!\!\text{ b}=\left[ {\begin{array}{*{20}{c}} 4 \\ 8 \\ {12} \\ {16} \end{array}} \right]

[m,n]=size(Mat_b)

\displaystyle \begin{array}{l}\text{m}=4\\\text{n}=1\end{array}

Suppose we want to construct Mat2 from Mat_r, Mat_y, Mat_g and Mat_b

Visually we can see that it is possible to concatenate Mat_y and Mat_r together:

We can check the sizes using MATLAB/Octave:

Mat_y

m=2, n=1

Mat_r

m=2, n=2

Because m, the number of rows match it is possible to concatenate the rows.

Mat_yr=[Mat_y,Mat_r]

\displaystyle \text{Mat }\!\!\_\!\!\text{ yr}=\left[ {\begin{array}{*{20}{c}} 3 & 1 & 2 \\ 7 & 5 & 6 \end{array}} \right]

[m,n]=size(Mat_yr)

\displaystyle \begin{array}{l}\text{m}=2\\\text{n}=3\end{array}

Now that we have Mat_yr, we can see that visually we can concatenate Mat_g and Mat_yr

Once again we can check the sizes using MATLAB/Octave:

Mat_g

m=2, n=3

Mat_yr

m=2, n=3

This time both m and n match but we are interested in column concatenation (joining the columns together) so it is n matching that is important.

Mat_yrg=[Mat_g;Mat_yr]

\displaystyle \text{Mat }\!\!\_\!\!\text{ yrg}=\left[ {\begin{array}{*{20}{c}} 9 & {10} & {11} \\ {13} & {14} & {15} \\ 3 & 1 & 2 \\ 7 & 5 & 6 \end{array}} \right]

[m,n]=size(Mat_gyr)

\displaystyle \begin{array}{l}\text{m}=4\\\text{n}=3\end{array}

Looking at:

Mat_gyr=[Mat_g;Mat_yr]

We know from before that:

Mat_yr=[Mat_y,Mat_r]

Therefore in one line

Mat_gyr=[Mat_g;[Mat_y,Mat_r]]

Now that we have Mat_gyr we can compare it to Mat_b and we can see that visually the number of rows match:

Once again we can check the sizes using MATLAB/Octave:

Mat_b

m=4, n=1

Mat_gyr

m=4, n=3

Because m, the number of rows match it is possible to concatenate the rows.

Mat2=[Mat_b,Mat_gyr]

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

[m,n]=size(Mat2)

\displaystyle \begin{array}{l}\text{m}=4\\\text{n}=4\end{array}

However we know:

Mat2=[Mat_b,Mat_gyr]

Mat_gyr=[Mat_g;Mat_yr]

So we can rewrite:

Mat2=[Mat_b,[Mat_g;Mat_yr]]

We also know:

Mat_yr=[Mat_y,Mat_r]

So we can rewrite:

Mat2=[Mat_b,[Mat_g;[Mat_y,Mat_r]]]

Note Matrices have to be rectangular with all rows having the same number of elements as each other and all columns having the same elements as each other. If you attempt to input a matrix which doesn't have matching dimensions you will get a dimensions mismatch error.

For example changing the following line:

Mat2=[Mat_b,[Mat_g;[Mat_y,Mat_r]]]

To:

Mat2=[Mat_b,[Mat_g;[Mat_y;Mat_r]]]

i.e. replacing a single comma with a semi-colon will produce the error:

For practice, you should try making Mat_b, Mat_r, Mat_g and Mat_y from Mat2. Then use these to recreate Mat in multiple lines and in a single line.


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 Octave/MATLAB we use the Apostrophe.

M=[1,2,3]

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

Mt=M'

\displaystyle \text{Mt}=\left[ {\begin{array}{*{20}{c}} 1 \\ 2 \\ 3 \end{array}} \right]

Mtt=Mt'

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

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

M=[1,2,3,4,5;6,7,8,9,10;11,12,13,14,15]

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

Next lets go ahead and transpose it:

Mt=M'

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

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.

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

row=[1,2,3,4] is pretty easy to type in but what happens if we want to look at all the rows of some hardware for example the XPS 13 9365 which has a row of 1800 pixels. We would be all day trying to type the 1800 numbers. Instead we can use the colon operator to type in row=[1:1800]=1:1800. If we want the column vector instead, we must make a row vector first and then transpose it col=[1:3200]' when reading such a large number of pixels we typically wouldn't output to the Command Window as most users can comprehend a huge list of numbers directly. Thus we would end the lines in a semi-colon.

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.

x=1:10

\displaystyle \text{x}=\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.

y=[1:8]'

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

x2=1:9.81

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

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:

x3=1:2:10

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

x4=1:0.1:1.8

\displaystyle \text{x4}=\left[ {\begin{array}{*{20}{c}} {1.0} & {1.1} & {1.2} & {1.3} & {1.4} & {1.5} & {1.6} & {1.7} & {1.8} \end{array}} \right]


The Colon and Logical Indexing

Let's revisit this four by four matrix

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

We have seen the Colon used for x=1:4 meaning x=[1,2,3,4]. The colon operator can also be used in logical indexing.

For Mat_r we are looking for column 1 and 2 and row 1 and 2. Thus we have the first input argument as [1:2]' and the second input argument as [1,2]

Mat_r=Mat([1:2]',[1:2])

\displaystyle \text{Mat }\!\!\_\!\!\text{ r}=\left[ {\begin{array}{*{20}{c}} 1 & 2 \\ 5 & 6 \end{array}} \right]

For Mat_y we are looking for column 1 and 2 and row 3. Thus we have the first input argument as [1:2]' and the second input argument as [3]

Mat_y=Mat([1:2]',[3])

\displaystyle \text{Mat }\!\!\_\!\!\text{ y}=\left[ {\begin{array}{*{20}{c}} 3 \\ 7 \end{array}} \right]

For such a small matrix this code doesn't save much time.

The key word end can also be used with logical indexing. For a column vector col=[1:10]' to select the first half you can use col([1:5]',1) and the second half col([6:end]',1).

For Mat_g we are looking for the last two columns [end-1:end]' and row 1,2 and 3 [1:end-1]. Thus we have the first input argument as [end-1:end]' and the second input argument as [1:end-1]

Mat_g=Mat([end-1:end],[1:end-1])

\displaystyle \text{Mat }\!\!\_\!\!\text{ g}=\left[ {\begin{array}{*{20}{c}} 9 & {10} & {11} \\ {13} & {14} & {15} \end{array}} \right]

The colon can also be used for short hand for 1:end, when used on it's own, i.e. in logical indexing it will select every element along that direction.

For Mat_b we are looking for at all columns and the last row. Thus we have the first input argument as : and the second input argument as [end]

Mat_b=Mat(:,[end])

\displaystyle \text{Mat }\!\!\_\!\!\text{ b}=\left[ {\begin{array}{*{20}{c}} 4 \\ 8 \\ {12} \\ {16} \end{array}} \right]

Note you have to use only : the following [:] or [:]' does not make sense to Octave/MATLAB.

When a Matrix M is indexed with a single colon 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{mat}=\left[ {\begin{array}{*{20}{c}} 1 & 2 & 3 & 4 \\ 5 & 6 & 7 & 8 \\ 9 & {10} & {11} & {12} \\ {13} & {14} & {15} & {16} \end{array}} \right]

M(:)

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


Linear Spacing

As we can see the Colon operator may miss the end point we specify if we are using decimal points.

x3=1:2:10

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

If we want both the start and the end point we are better to use the linspace function which has the form OutputArray=linspace(x1,xe,n) and has three input arguments, x1 the first number, xe the last number and n the number of points, which it will use to create the OutputArray

x3_2=linspace(1,10,5)

\displaystyle \begin{array}{l}\text{x4}=\left[ {\begin{array}{*{20}{c}} {1.0} & {1.1} & {1.2} & {1.3} & {1.4} & {1.5} & {1.6} & {1.7} & {1.8} \end{array}} \right]\\\text{x3 }\!\!\_\!\!\text{ }2=\left[ {\begin{array}{*{20}{c}} 1 & {3.25} & {5.5} & {7.75} & {10} \end{array}} \right]\end{array}


Reshape

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 the reshape function. This has the form Moutput=reshape(Minput,[m,n]) where Minput is the input matrix and m and n are the number of rows and number of columns you wish to reshape the output matrix Moutput to. In this case we can use

x=1:10

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

Then:

C=reshape(x,[5,2])

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

We could also use the data from C to reshape

D=reshape(C,[2,5])

\displaystyle \text{D}=\left[ {\begin{array}{*{20}{c}} 1 & 3 & 5 & 7 & 9 \\ 2 & 4 & 6 & 8 & {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. Reshape similarly works in column order but then creates a new matrix using the specified m and n of the other input arguments.

We have previously made the matrix that is row ordered. We can recreate it using reshape but because it is row ordered we will need to transpose it.

Mat=[1,2,3,4;5,6,7,8;9,10,11,12;13,14,15,16]

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

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


A Matrix of 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 do you do that? You do that by sending zeros to every pixel on the screen. The function zeros has the form Zmn=zeros(m,n) where the input argument m is the number of rows and n is the number of columns and Zmn is the output zero matrix. For example:

Z52=zeros(5,2)

\displaystyle \text{Z52}=\left[ {\begin{array}{*{20}{c}} 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 \end{array}} \right]


A Matrix of ones

A similar matrix is the ones matrix which allows one to make a matrix full of ones. Ones can of course be multiplied by any number to get a matrix where all elements have that number. Again if we think of the screen, doing this would create a monochrome image across the screen. The function zeros has the form Omn=oneos(m,n) where the input argument m is the number of rows and n is the number of columns and Omn is the output ones matrix. For example:

O33=ones(3,3)

\displaystyle \text{O33}=\left[ {\begin{array}{*{20}{c}} 3 & 3 & 3 \\ 3 & 3 & 3 \\ 3 & 3 & 3 \end{array}} \right]


Creating a 3D Array

A 3D array is a series of Matrices. To create one we can first create some 2D arrays of equal dimensions.

Page1=reshape([1:12],[3,4])'

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

Page2=reshape([13:24],[3,4])'

\displaystyle \text{Page2}=\left[ {\begin{array}{*{20}{c}} {13} & {14} & {15} \\ {16} & {17} & {18} \\ {19} & {20} & {21} \\ {22} & {23} & {24} \end{array}} \right]

Page3=reshape([25:36],[3,4])'

\displaystyle \text{Page3}=\left[ {\begin{array}{*{20}{c}} {25} & {26} & {27} \\ {28} & {29} & {30} \\ {31} & {32} & {33} \\ {34} & {35} & {36} \end{array}} \right]

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 colons as all elements are selected. The third input will be the page number which will be assigned to 1, 2 and 3 respectively.

Book(:,:,1)=Page1

Book(:,:,2)=Page2

Book(:,:,3)=Page3